N
N
Nikolay Semenov2016-12-08 20:17:54
.NET
Nikolay Semenov, 2016-12-08 20:17:54

Sorting does not work correctly. where is the mistake?

there is such a sorting
array 1256, 4558, 221, 665, 789, 23 comes into it
and it comes out like this 789, 665, 4558, 23, 221, 1256
and as I understand it sorts by the first digit 7, 6, 4, etc .d.

class InsertionSorting
    {
        public string[] SortUp(string[] lane)
        {
            string temp;
            int j;
            for (int i = 1; i <= lane.Length - 1; i++)
            {
                temp = lane[i];
                j = i - 1;
                while (j >= 0 && String.CompareOrdinal(lane[j], temp) < 0)
                {
                    lane[j + 1] = lane[j];
                    j--;
                }
                lane[j + 1] = temp;
            }
            return lane;
        }
    }

Guys what's wrong?
the task is to sort by lexicographic order (by character codes)
thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-12-08
@senal

The order is just lexicographic, and if you want to sort as numbers then:

lane.Select(s => int.Parse(s)).OrderByDescending(s => s);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question