Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a collection of numeric Strings

Tags:

c#

.net

sorting

I need the ability to sort a collection of customers which contains a property of numeric string.

How Can I sort the below collection by code in numeric order. Again Code is a string.

           class Program
           {
              static void Main(string[] args)
              {
                 SortableObservableCollection<Customer> customerList = new SortableObservableCollection<Customer>();
                 customerList.Add(new Customer() {Name = "Jo", Code = "1"});
                 customerList.Add(new Customer() { Name = "Jo", Code = "10" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "11" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "9" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "7" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "12" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "13" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "2" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "5" });
                 customerList.Add(new Customer() { Name = "Jo", Code = "7" });

                 //Order them by Code How
              }
           }

           public class Customer
           {
              public string Name { get; set; }
              public string Code { get; set; }
           }

Thanks for any suggestions

like image 376
user9969 Avatar asked Sep 15 '10 15:09

user9969


2 Answers

Option 1: implement IComparer<Customer> and parse the Code within that

Option 2: use LINQ to do the same thing:

customerList = customerList.OrderBy(c => int.Parse(c.Code)).ToList();

Option 3: change the Customer class so that a numeric value is stored as a numeric type :)

EDIT: As noted, this will throw an exception if you give it a customer with a non-numeric code.

Also, I'm calling ToList() because I assumed you still needed it to be a list. If you just need to iterate over the results in order, then use:

IEnumerable<Customer> ordered = customerList.OrderBy(c => int.Parse(c.Code));

Note that if you iterate over that twice, however, it will perform all the parsing and sorting twice too.

like image 169
Jon Skeet Avatar answered Nov 13 '22 13:11

Jon Skeet


If the keys are always just numbers and those numbers are always convertible to ints then it is pretty straightforward. Just convert them to ints.

var sorted = from customer in customerList orderby Int32.ParseInt(customer.Code) select customer;

If any of them are not ints then this will crash.

Another way to do it is to pad the string out with leading zeros:

var sorted = from customer in customerList orderby PadToTenDigits(customer.Code) select customer;

where PadToTenDigits is a method left as an exercise, that turns "1" into "0000000001" and "1000" into "0000001000", and so on.

If you have to do true "numeric" sorting on complex codes, where, say, code "A300-B" sorts before "A1000-XYZ", then you have a harder problem on your hands. The trick there is to break up the code into "parts" and do a stable sort on each part. Basically, a radix sort.

like image 30
Eric Lippert Avatar answered Nov 13 '22 12:11

Eric Lippert