How to sort an array of text and number ? (C#)

Hello,
I’m trying to sort an array of text and number like “12345.abcde” , I need to have it in this format and can’t split it and I want to sort them. I tried Array.Sort but it has no result, also I tried a bubble sort like this:

while(foundone)
			{
				foundone = false;
				for(int ii = 0; ii < friends_array.Length - 1; ii++)
				{
					if(int.Parse(friends_array[ii]) > int.Parse(friends_array[ii + 1]))
					{
						temp_obj = friends_array[ii + 1];
						friends_array[ii + 1] = friends_array[ii];
						friends_array[ii] = temp_obj;
						foundone = true;
					}
				}
			}

But it won’t work neither. what shall I do ?

I don’t quite understand what kind of sorting you need. Could you show us a sample array before and after sorting?

perhaps you can just sort them by their ascii code… only problem is that . (46) < then 1 (48)

I goes it shouldn’t be to hard to figure out how to get the “.” in the right place.

private static void Main()
{
	var before = "8375460923985.iuoeantytalmpuae";

	var chars = before.ToCharArray();

	var length1 = before.IndexOf('.');
	var length2 = chars.Length - length1 - 1;

	Array.Sort(chars, 0, length1);
	Array.Sort(chars, length1 + 1, length2);
			
	var after = new string(chars);

	Console.WriteLine("Before: {0}", before);
	Console.WriteLine("After : {0}", after);
	Console.ReadLine();
}

Before: 8375460923985.iuoeantytalmpuae
After : 0233455678899.aaaeeilmnopttuuy

Thanks ShadoX, that’d be so hard >.<
Thanks Alexzzzz I didn’t mean sorting a string :slight_smile:

Example:
Before
[567.gghj]
[345433.bgffcd]
[112233.aabbcc]

After
[345433.bgffcd]
[112233.aabbcc]
[567.gghj]

Though I found a way to get rid of that dot and it was using an array of class so I could sort it using bubblesort.
like this:

public class Friends {
public string name=""
public string score=""
}

public Friends[] my_list;

@shahroozal
Is ur problem solved?

probably not… all you need to do to get the ASCII value is a cast to int…
ex.: (int) ‘A’ == 65

but I goes there’s no need for that since alexzzzz already answered the question.

Create your own IComparer…

The example is a bit long winded, but it shows the idea.

Alexzzzz’s answer was not what I was looking for though.

I solved it but the main question is still there.

Yes that’s the answer! Thanks a lot JohnnyA :slight_smile:

private static void Main()
{
	var before = new[] { "567.gghj", "345433.bgffcd", "112233.aabbcc", "98346.alkjgr", "81395.krlgjerg", "9087689.sdfew" };
	Console.WriteLine("Before:");
	foreach (var entry in before)
	{
		Console.WriteLine(entry);
	}
	Console.WriteLine();

	var after = before.OrderByDescending(entry => int.Parse(entry.Substring(0, entry.IndexOf('.'))));

	Console.WriteLine("After:");
	foreach (var entry in after)
	{
		Console.WriteLine(entry);
	}
	Console.WriteLine();
	Console.ReadLine();
}

Before:
567.gghj
345433.bgffcd
112233.aabbcc
98346.alkjgr
81395.krlgjerg
9087689.sdfew

After:
9087689.sdfew
345433.bgffcd
112233.aabbcc
98346.alkjgr
81395.krlgjerg
567.gghj

That’s so clever ! thanks man.