Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-15
8
Medium Priority
?
992 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 2000 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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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