Search Unity

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

Discussion in 'Scripting' started by shahroozal, Aug 21, 2012.

  1. shahroozal

    shahroozal

    Joined:
    Feb 14, 2012
    Posts:
    68
    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:
    Code (csharp):
    1. while(foundone)
    2.             {
    3.                 foundone = false;
    4.                 for(int ii = 0; ii < friends_array.Length - 1; ii++)
    5.                 {
    6.                     if(int.Parse(friends_array[ii]) > int.Parse(friends_array[ii + 1]))
    7.                     {
    8.                         temp_obj = friends_array[ii + 1];
    9.                         friends_array[ii + 1] = friends_array[ii];
    10.                         friends_array[ii] = temp_obj;
    11.                         foundone = true;
    12.                     }
    13.                 }
    14.             }
    But it won't work neither. what shall I do ?
     
    Last edited: Aug 21, 2012
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I don't quite understand what kind of sorting you need. Could you show us a sample array before and after sorting?
     
  3. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    perhaps you can just sort them by their ascii code.. only problem is that . (46) < then 1 (48)

    http://www.asciitable.com

    I goes it shouldn't be to hard to figure out how to get the "." in the right place.
     
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (csharp):
    1.  
    2. private static void Main()
    3. {
    4.     var before = "8375460923985.iuoeantytalmpuae";
    5.  
    6.     var chars = before.ToCharArray();
    7.  
    8.     var length1 = before.IndexOf('.');
    9.     var length2 = chars.Length - length1 - 1;
    10.  
    11.     Array.Sort(chars, 0, length1);
    12.     Array.Sort(chars, length1 + 1, length2);
    13.            
    14.     var after = new string(chars);
    15.  
    16.     Console.WriteLine("Before: {0}", before);
    17.     Console.WriteLine("After : {0}", after);
    18.     Console.ReadLine();
    19. }
    20.  
    Before: 8375460923985.iuoeantytalmpuae
    After : 0233455678899.aaaeeilmnopttuuy
     
    Last edited: Aug 21, 2012
  5. shahroozal

    shahroozal

    Joined:
    Feb 14, 2012
    Posts:
    68
    Thanks ShadoX, that'd be so hard >.<
    Thanks Alexzzzz I didn't mean sorting a string :)


    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:
    Code (csharp):
    1. public class Friends {
    2. public string name=""
    3. public string score=""
    4. }
    5.  
    6. public Friends[] my_list;
     
    Last edited: Aug 22, 2012
  6. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    @shahroozal
    Is ur problem solved?
     
  7. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    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.
     
    Last edited: Aug 22, 2012
  8. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
  9. shahroozal

    shahroozal

    Joined:
    Feb 14, 2012
    Posts:
    68
    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 :)
     
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (csharp):
    1.  
    2. private static void Main()
    3. {
    4.     var before = new[] { "567.gghj", "345433.bgffcd", "112233.aabbcc", "98346.alkjgr", "81395.krlgjerg", "9087689.sdfew" };
    5.     Console.WriteLine("Before:");
    6.     foreach (var entry in before)
    7.     {
    8.         Console.WriteLine(entry);
    9.     }
    10.     Console.WriteLine();
    11.  
    12.     var after = before.OrderByDescending(entry => int.Parse(entry.Substring(0, entry.IndexOf('.'))));
    13.  
    14.     Console.WriteLine("After:");
    15.     foreach (var entry in after)
    16.     {
    17.         Console.WriteLine(entry);
    18.     }
    19.     Console.WriteLine();
    20.     Console.ReadLine();
    21. }
    22.  
    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
     
  11. shahroozal

    shahroozal

    Joined:
    Feb 14, 2012
    Posts:
    68
    That's so clever ! thanks man.