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

Custom Sort a list

Discussion in 'Scripting' started by unity_91BnGyqxSYUPdQ, Aug 7, 2018.

  1. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    Hello Guys,

    I want to sort a list in a custom manner as follows

    Given List Order = 1,2,3,4,5

    ***** CORRECTION : Required List Order = 2,3,5,4,1

    please let me know how this can be achieved.

    thank you all in advance.
     
    Last edited: Aug 7, 2018
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If the given list was [ 10,9,8,7,6 ], what would the result be? Would it be [ 6, 8, 10, 9, 7 ], or would it be [ 10, 8, 6, 7, 9 ]?
     
  3. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The OP has not explained it well but there is a pattern to their requirement - at least, it does not appear to be random. What I am unsure about is whether the final arrangement is based on the values of the input elements or on their original positions. Hence my request for a clarification. :)
     
    FMark92 likes this.
  5. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    If you want to use custom rules to sort a list, take a look at this thread. It explains several methods of doing so.
     
  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Oh, I've just now noticed that it's not random. It's consecutive odd numbers increasing and then even numbers decreasing. I'm guessing it only looks so pretty when working with a sequence of consecutive numbers starting with an odd number.

    And of course I applaud your belief that OP will log in for a second time.
     
    Doug_B likes this.
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    :)
     
    FMark92 likes this.
  8. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    @Doug_B the output would be [ 10, 8, 6, 7, 9 ]
     
  9. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    Sorry for the late reply guys
     
  10. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    also if the given order is : 1,2,3,4,5,6,7,8
    Required List Order = 2,4,6,8,7,5,3,1
     
  11. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    Are you asking on how to sort a list with custom rules, or the actual code of your sorting rules?
     
  12. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    I want to know how to sort a list in that order or if there is any code available directly do do the same will be of great help.
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So to clarify the logic it is this :-
    1. Start with old-list of n elements ( index 0 to n-1 ).
    2. Want to populate new-list from old-list.
    3. Move element old-list[ n-1 ] to new-list[ n / 2 ]
    4. Move element old-list[ n-2 ] to new-list[ ( n / 2 ) + 1 ]
    5. Move element old-list[ n-3 ] to new-list[ ( n / 2 ) - 1 ]
    6. Repeat steps 4 & 5 incrementing the "+/- 1" on each loop until exhaustion of the source list.
    Do you agree?
     
  14. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    Yes thats correct

    basically I want the last element of any list to be placed as the middle element of the list and have the second last and third last element to the middle elements left and right.

    its a bit hard for me also to explain sorry.:(
     
  15. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    This is a bit messy. I've quickly tested it (with [1, 2, 3]). I would recommend testing it more thoroughly yourself though:
    Code (CSharp):
    1. public List<int> Sort(List<int> source)
    2. {
    3.     List<int> result = new List<int>(source);
    4.     int sign = -1;
    5.  
    6.     for (int i = 0; i < source.Count; i++)
    7.     {
    8.         result[(source.Count / 2) + (sign * ((i + 1) / 2))] = source[source.Count - i - 1];
    9.         sign = -sign;
    10.     }
    11.  
    12.     return result;
    13. }
     
  16. unity_91BnGyqxSYUPdQ

    unity_91BnGyqxSYUPdQ

    Joined:
    Aug 7, 2018
    Posts:
    25
    Thanks you DOUG_B this is exactly what I needed perfect works like a charm:D:D:D:D:D:D:D:D:D:D:D:D

    I added a simple line in it to make it work for even count list

    Code (CSharp):
    1.     public List<int> Sort(List<int> source)
    2.     {
    3.         List<int> result = new List<int>(source);
    4.         int sign = -1;
    5.  
    6.         for (int i = 0; i < source.Count; i++)
    7.         {
    8.             int index = (source.Count / 2) + (sign * ((i + 1) / 2));
    9.             int index2 = (source.Count - i - 1);
    10.             if(index < source.Count) // this checks if the index is less the the count if yes then only proceeds.
    11.                 result[index] = source[index2];
    12.             sign = -sign;
    13.         }
    14.  
    15.         return result;
    16.     }

    Thank you again :D:D:D:D:D
     
    Doug_B likes this.