Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort Integer Strings?

Tags:

c#

.net

sorting

I am facing a strange problem while sorting a list of strings with integer values. However some values could be prefixed with some characters.

e.g.

// B1, 5, 50, A10, 7, 72, B3, A1, A2

There are basically page numbers and should be sorted like:

// A1, A2, A10, B1, B3, 5, 7, 50, 72

But if I use default string sorting then these will be sorted like

// A1, A10, A2, B1, B3, 5, 50, 7, 72

Any solution for this in C#?

like image 546
Ramesh Soni Avatar asked Apr 24 '09 10:04

Ramesh Soni


People also ask

How do I sort an integer string?

Convert each element in the String array obtained in the previous step into an integer and store in into the integer array. The sort() method of the Arrays class accepts an array, sorts the contents of it in ascending order. Sort the integer array using this method.

How do you sort integers?

Integer sorting algorithms including pigeonhole sort, counting sort, and radix sort are widely used and practical. Other integer sorting algorithms with smaller worst-case time bounds are not believed to be practical for computer architectures with 64 or fewer bits per word.

Can we sort string and integer in Java?

Sort String in Java String in Java is immutable. There is no direct method to sort a string in Java. You can use Arrays, which has a method CharArray() that will create a char input string and using another method (Arrays. sort(char c[]) , we can easily sort.


1 Answers

You're looking for the Alphanum algorithm. Fortunately for you, a number of implementations exist already. See here.

like image 135
John Feminella Avatar answered Sep 30 '22 13:09

John Feminella