Solved

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

Posted on 2009-07-15
8
912 Views
Last Modified: 2012-05-07
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
Comment
Question by:Dynotoe
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24863509
Array.Sort(YourArrayObject);
0
 

Author Comment

by:Dynotoe
ID: 24863546
Hi Ryan,

What would be my "YourArrayObject" argument?
0
 
LVL 8

Expert Comment

by:NotLogical
ID: 24863574
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
 

Author Comment

by:Dynotoe
ID: 24863637
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 8

Expert Comment

by:RyanAndres
ID: 24863690
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
 
LVL 8

Accepted Solution

by:
NotLogical earned 500 total points
ID: 24863817
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
 

Author Comment

by:Dynotoe
ID: 24863822
"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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24863940
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now