Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

C# - Sorting an string ArrayList by the "Integer" number of the first comma delimited field

Hi everyone,

I have a rather large strig arraylist where each element represents a comma delimited text line which has a integer (long int) first field in the string.  For excample:

"148, Buy, open"
"35, sell, close"
Can someone show me how I can sort the arraylist by the first field integer number so that it is in "Numeric" oder?

Please let me give my gratitude in advance.

Thx very much.

Best - Dynottoe
0
Dynotoe
Asked:
Dynotoe
  • 3
  • 2
  • 2
  • +1
1 Solution
 
RyanAndresCommented:
Array.Sort(YourArrayObject);
0
 
DynotoeAuthor Commented:
Hi Ryan,

What would be my "YourArrayObject" argument?
0
 
NotLogicalCommented:
Hello Dynottoe,

Fortunately for you, with the number being first, your list will sort "automagically"! If your number came at the end of the string, this would be a little more difficult. This is because when the sort function looks at strings in a left-to-right fashion, the numbers will be evaluated first. For example:

Original list:
"148, Buy, open"
"35, sell, close"
"35, Buy, another open"

Sorted list:
"35, Buy, another open"
"35, sell, close"
"148, Buy, open"

You can perform the sort in the following fashion:

YourArrayList.Sort();

Now, what is neat about Sort is that you can pass it a custom function for determining order of strings being sorted...

Cheers,

NotLogical
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DynotoeAuthor Commented:
Hi Not Logical,

Well is there a way to ignor he rest of fthe csv string in the sort process?  My arraylist is huge and I need speed.  Any ideas?
0
 
RyanAndresCommented:
Use a generic List rather than an ArrayList for type safety and increased performance.

Since you are reading in each line anyway you should just do a sort on the entire string because in the underlying code it stops  the comparison once it finds a difference.
List<string> str = new List<string>();
str.Add("148, Buy, open");
str.Add("35, sell, close");
str.Sort();

Open in new window

0
 
NotLogicalCommented:
Hi Dynotoe,

There are a couple of caveats with the built-in Sort function:
- .net is quite well optimized, so its sort is very fast for a "general case"
- You could write your own custom comparison function for the Sort function, and limit to only sorting on everything up to the first comma (for example). This may yield some other interesting effects like:

Unsorted:
   "1, C"
   "1, Z"
   "1, A"

Sorted:
   "1, C"
   "1, Z"
   "1, A"

No difference! This would be because the numbers are already in order... Would that be okay?

How many entries are you dealing with? Is it possible to sub-sort a small portion of the list, or is the entire list changed every time? How about caching a copy of the sorted list, and only updating it when an element changes?

There are a couple of other thoughts which come to mind. Perhaps instead of sorting the list after you added all elements to it, can you add the elements in order? This could be helped using the BinarySearch function of ArrayList.

Another thought is that maybe this ArrayList is not the most efficient storage structure for what you are trying to do. Perhaps creating a structure which defines the int and string, and then managing them as such, might make more sense? A sort would be dead-easy then, as you would be comparing integers, not entire strings!

Please let me know if you need a little more help...

NotLogical
0
 
DynotoeAuthor Commented:
"it stops  the comparison once it finds a difference."

of course why wouldn't it.  Very cool.

Ryan Check in 5 minutes, becasue I am askingh another sort question for 500 points but shouldn't be combined with this question.  If you can help me with that as well.

Thanks - Sean
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Also consider that you are still sorting a STRING so it will not evaluate the actual value of the integer.

Thus "2000000" will come before "30" because "2" is less than "3".

If you want it to correctly sort by the NUMERIC VALUES then you'll have to actually parse out the integer portion and properly convert it to a numeric data type do the comparison.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now