Home > Back-end >  Writing my own sort() function for 2D array
Writing my own sort() function for 2D array

Time:03-20

I need to write my own sort() function for an assignment. Let's start off with this list to sort:

data_list = [-5, -23, 0, 100, 23, -6, 23, 67]

I create a new list, which will be sorted, empty for now:

new_list = []

And here is some code that I found on stackoverflow, thank you community :)

def sortData(lista):
    while lista:
        minimum = lista[0]  # arbitrary number in list 
        for x in lista: 
            if x < minimum:
                minimum = x
        new_list.append(minimum)
        lista.remove(minimum) 
    return new_list

sortData(data_list)        
print(new_list)

The result is

new_list = [-23, -6, -5, 0, 23, 23, 67, 100]

So far so good. But what I need is to sort a 2D list, and I need to sort it by the last column, in decreasing order. Here is the 2D list

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],
         ['barbados', 6,34,-39],['japan', 12,8,16]]

As you can tell, I must NOT include the first row in the sort, obviously. So the sort needs to be starting at the second row, and the data needs to be sorted by the last column in decreasing order. The desired result would be:

listaSorted = [['countries', 2019, 2021, 2022],['japan', 12,8,16],['aruba', 2,13,8],['barbados', 6,34,-39]]

If i use the current code, i just get the list sorted by the first column and including the first row, see here:

def sortData(lista):
    while lista:
        minimum = lista[0]  # arbitrary number in list 
        for x in lista: 
            if x < minimum:
                minimum = x
        new_list.append(minimum)
        lista.remove(minimum) 
    return new_list

listToSort = [['countries', 2019, 2021, 2022],['japan', 12,8,16],['aruba', 2,13,8],['barbados', 6,34,-39]]
new_list = []

sortData(listToSort)        
print(new_list)

new_list = [['aruba', 2, 13, 8], ['barbados', 6, 34, -39], ['countries', 2019, 2021, 2022], ['japan', 12, 8, 16]]

So that does not work :(

I cannot use any imported modules. And I have been advised not to remove elements from the first list. I agree. That might mess up the earlier parts of my program. I am pretty stuck, any help would be awesome!

CodePudding user response:

Don't compare x to minimum, but x[3] to minimum[3], which is the last column. Then change the comparison order as you want a maximum

def sortData(lista):
    new_list = []
    while lista:
        maximum = lista[0]
        for x in lista:
            if x[3] > maximum[3]:
                maximum = x
        new_list.append(maximum)
        lista.remove(maximum)
    return new_list

listToSort = [['countries', 2019, 2021, 2022], ['japan', 12, 8, 16], ['aruba', 2, 13, 8], ['barbados', 6, 34, -39]]
new_list = sortData(listToSort)
print(new_list)

Also don't define global variable to be used in method, instanciate new_list in the method then retrieve it in the main code


For your culture, with builtin sorted it can be done with

new_list = sorted(listToSort, key=lambda x: x[3], reverse=True)
print(new_list)

CodePudding user response:

Your comparison criterion is wrong. Change x < minimum to x[-1] < minimum[-1]. This will sort the 2-D array by the last column in increasing order. To sort in decreasing order, change < to >.

BTW, the sorting function you found is garbage. It's the so called selection sort, which is suboptimal, and it's a terrible implementation of that sort due to its use of list.remove. You'd better use the built-in sorted. Do this

sorted(<YOUR ARRAY>, key=lambda x: x[-1], reverse=True)
  • Related