# ordering strings alphanumerically



## ste2425 (Jul 22, 2013)

Hi all

I have a question for you all. 

Im trying to order a set of input data. Its a list of strings some containing just number, some just letters and some a mixture. Im coding in c# using i believe asp.net. Its a .net technology. Sorry for the vagueness there but i have only been using it for half a week and my dam windows has died and im writing this from a Ubuntu live cd (gotta love linux). 

Any who ive currently managed to split the input list into three seperate list using some regex and converted the numbers into int using int.Parse(). Then ordered the numbers using .order() so that side of things is grand. I now have a list of strings containing just numbers which i order again using .order() and it orders them alphabetically, then using .addrange() i can combine the two together listing all the numbers then all the letters alphabetically at the end. However ive come a bit unstuck with the strings that contain letters and numbers.

for example i have

2test
3test
11test
4ab
1ab
32ab

which i then need listed as

1ab
4ab
32ab
2test
3test
11test

the issue is the number could be on the end or at the front. I want them to be ordered primarely alphabetically then if theres many words starting with T for example they are block together and run numercially. 

So when its all finished i end up with a list like this

1
2
5
8
22
32
a
abc
bhg
2bh
bh4
bh5
h
j
3j
j5


Hope all this makes sense im in the process of fixing windows 8 AGAIN so should be able to provide more info if needed. Hope you all can shed some light onto this. Im stuck on ordering the string containing both letters and numbers


----------



## FordGT90Concept (Jul 22, 2013)

The easiest way to accomplish that is via a custom IComparer or IComparable.  There's an example here:
http://msdn.microsoft.com/en-us/library/234b841s.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Basically your comparer returns an 32-bit signed integer that returns 0 if a and b are equal, greater than zero if a should be higher than b, and less than zero if a should be less than b.


Of course the easiest solution may be to move the numbers to the rear of the string then just use the basic string compare.


----------



## ste2425 (Jul 22, 2013)

Hi thanks. Could you explain the compare a greater then b bit further I don't fully understand what you mean by a greater then b. my comps fully died now so carnt google it. Having to use dam iphone app


----------



## FordGT90Concept (Jul 22, 2013)

Let me just pull an example from one of my programs...

```
using System.Collections.Generic;

namespace JbnLib.Shared
{
    public class OffsetLengthPairComparer : IComparer<OffsetLengthPair>
    {
        public int Compare(OffsetLengthPair a, OffsetLengthPair b)
        {
            if (a.Offset > b.Offset)
                return 1;
            else if (a.Offset < b.Offset)
                return -1;
            else
                return 0;
        }
    }
}
```
It compares a to b and depending on what it returns, places a before b, a after b, or no change.  To invert it, simply swap that negative sign.

In your program, you'll want to first compare the string component, then compare the numeric component.  Remember, if you use Convert.ToInt32 (or similar), it ignores all non-numeric characters.


----------



## ste2425 (Jul 22, 2013)

Oh now I'm with you. Stick that in a for each loop going through my lists and itl place them in order just condition like you said to go through string then numerical components. Great. Thank you very much.  when I get my computer up tomorrow ill have a play.


----------



## FordGT90Concept (Jul 22, 2013)

You don't loop it, it does via .Sort(IComparer).  In my example, it would like like...

```
List<string> list = new List<string>();
list.Add("2");
list.Add("1");
list.Add("0");

//sort it
OffsetLengthPairComparer comparer = new OffsetLengthPairComparer();
list.Sort(comparer);
```
Obviously my comparer doesn't work for strings because it is for a custom type but, you get the idea.


----------

