Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

String Array Sort Problem

Discussion in 'Scripting' started by manlaikin1994, Mar 20, 2016.

  1. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System;
    6.  
    7.  
    8. public class CompareStrings : IComparer<string>
    9. {
    10.     // Because the class implements IComparer, it must define a
    11.     // Compare method. The method returns a signed integer that indicates
    12.     // whether s1 > s2 (return is greater than 0), s1 < s2 (return is negative),
    13.     // or s1 equals s2 (return value is 0). This Compare method compares strings.
    14.     public int Compare(string s1, string s2)
    15.     {
    16.         return string.Compare(s1, s2, true);
    17.     }
    18. }
    19.  
    20. public class testSort : MonoBehaviour
    21. {
    22.     public GameObject[] gos;
    23.     public string[] unsortedArray;
    24.  
    25.     void Update()
    26.     {
    27.         if(Input.GetKeyDown(KeyCode.A))
    28.         {
    29.             unsortedArray = new string[gos.Length];
    30.             for(int i = 0; i < gos.Length; i++)
    31.             {
    32.                 unsortedArray[i] = gos[i].name;
    33.             }
    34.  
    35.  
    36. //            Array.Sort(gos, delegate(GameObject x, GameObject y)
    37. //                {
    38. //                    return String.Compare(x.name, y.name);
    39. //                });
    40.            
    41.  
    42.         var sortedArray = unsortedArray.OrderBy(a => a, new CompareStrings());
    43.  
    44. //            IEnumerable<GameObject> gosQuery =
    45. //                from go in gos
    46. //                orderby go ascending
    47. //                select go;
    48.  
    49.             foreach(var element in sortedArray)
    50.             {
    51.                 Debug.Log(element);
    52.             }  
    53.         }
    54.     }
    55. }
    56.  
    螢幕快照 2016-03-20 下午2.01.42.png


    I want to do Exactly the same things with hierarchy's alpha sort,

    however the output is not the same,

    i have tried a lot of function that found on internet and MSDN

    such as delegate(T x, T y)
    return String.Compare(x.name, y.name)

    and exactly copy from Unity API with alpha sort,

    However the output is not what i want.

    I don't why, Help me PLZ!!!!

    PS: The output what i want is 1-> 2->3 ......25 > 26
     
    Last edited: Mar 20, 2016
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    You should have posted that first picture as code box, not picture. Now i can't copy-paste :(

    But maybe:
    Code (CSharp):
    1. return string.Compare(s1, s2, System.StringComparison.Ordinal);
     
  3. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Updated, its doesn't work, same output
     
  4. gaweph

    gaweph

    Joined:
    Sep 2, 2012
    Posts:
    26
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    You have it done so far already, i feel like it just comes down to this:
    Code (CSharp):
    1.     public int Compare(string s1, string s2)
    2.     {
    3.         return string.Compare(s1, s2, true);
    4.     }
    "Ordinal" usually means numeric, but seems like it means different thing here for C#. You'll have to change that line other way then.

    Is it "AlphaNumeric" comparison or just "Numeric" you are after? You may get simpler answer if it's just numeric. Convert s1 and s2 to number and return value based on them.
     
  6. gaweph

    gaweph

    Joined:
    Sep 2, 2012
    Posts:
    26
    I believe the problem here is that the values are alphanumeric and therefore cannot be converted to numbers.