1

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!

6
  • As you say yourself, " the data needs to be sorted by the last column." - so try to reflect that in your code... Commented Mar 20, 2022 at 9:51
  • So the 2D list should be sorted in reverse order by the last column key? Just double checking, since it's opposite of your first list, and not mentioned Commented Mar 20, 2022 at 9:51
  • Can you use enumerate and zip? Commented Mar 20, 2022 at 9:51
  • Use .reversed method to sort from last colomn Commented Mar 20, 2022 at 9:52
  • yes that is true @kcsquared! it needs to be sorted from max positive value down to max minimum value, for the last column in the array. Thank you for pointing that out. Commented Mar 20, 2022 at 9:53

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @azro, that code worked. Yeah, I used sorted() and lambda to make my program work till I got this fixed. :)
1

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)

3 Comments

I hear you @yzhang, and I will need to make a copy of my listToSort so that, when elements are removed, it does not crash my earlier program code.
@LabCoat You don't need to make a copy as sorted will return a new copy without touching your original one.
yes @yzhang, but my defined function will though, and as I am using my function, I will need to make a deep copy of that original list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.