Quicksort a list of list by column

Viewed 69

i would like to quicksort a list of list which i generatet from a csv via pandas. I want to sort it by a specific column e.g. number oder height.

The Data looks like this:

Number Name Height Width Weigth
1 Hatity 66.23 57.28 124.87
2 Bamity 22.56 843.7 123.67
3 Asoka 45.66 234.3 523.29

I was already able to sort it by the second column with this code:

def quickSort(list):
  if not list:
      return list
  pivot = list[0]
  lesser = quickSort([x for x in list[1:] if x[1] < pivot[1]])
  greater = quickSort([x for x in list[1:] if x[1] >= pivot[1]])
  return lesser + [pivot] + greater
print(quickSort(product_list))

This is working fine for sorting by name. But i dont know what to change to sort by any other column. It would be great if somebody could help me.

1 Answers
product_list = [[1,10],[3,8],[2,9]]

def quickSort(list, compare_fn):
  if not list:
      return list
  pivot = list[0]
  lesser = quickSort([x for x in list[1:] if compare_fn(x, pivot)], compare_fn)
  greater = quickSort([x for x in list[1:] if not compare_fn(x, pivot)], compare_fn)
  return lesser + [pivot] + greater
print(quickSort(product_list, lambda x,y: x[0] < y[0]))
Related