How can I sort an array of strings, if there is an array, where sorting is followed in order, as I need. In my case it is:

  1. T-90
  2. T-100
  3. T-110
  4. T-120
  5. Tv
  6. No analysis
  7. Not standard
  8. Defective

And I have my own array, which I need to sort by the list above. How to do it?

  • 3
    It is possible for more details with an example of what we have and what to get at the output) bring a small piece pliz - Ale_x
  • I correctly understand that: - You have a certain list of strings / arrays / tuples / objects that you want to sort. - Objects in your list as one of their fields / elements contain a string. - This line can take one of 8 values ​​described in the question. - You want to sort your list by this string value, in accordance with the priorities of possible values ​​(first T-90, then T-100, and so on). - Nofate

3 answers 3

Very sketchy. Get the function of comparing two elements A and B, which would be:

  1. would look for position A in the reference array (a)
  2. would search for position B in the reference array (b)
  3. would return (ab), that is,> 0 if A is "greater than" B, 0 if A "is equal to" B, <0 if A is "less than" B

Well, continue to use this feature as an aid to QuickSort, for example. With cases where A or B are not in the reference array, or there is a partial match, or something else, figure it out yourself :)

    Em..

    I don't even know how this can sort

    Тв Без анализа Не стандартная Брако 

    First TV, then B, then H, then again B ...

      The first thing that comes to mind: if the data is taken from the database, then enter additional fields for sorting. In any case, there must be a sign by which it is sorted.