Solved

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

Posted on 2009-07-15
8
952 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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