Solved

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

Posted on 2009-07-15
8
923 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

15 Experts available now in Live!

Get 1:1 Help Now