-2

An example .csv file would be (headings: "Name, Provider, Months remaining")

John,O2,12
Adam,EE,11
Arnold,O2,14
Sarah,Voxi,9
Mitch,EE,4
Kirstie,GiffGaff,10
Elise,Voxi,3
Eleanor,GiffGaff,7

How would you find the total remaining months for each provider; then list the provider's with total months in ascending order? Using ideally a bubble sort?

Edit: This is without any libraries, no sort(), pandas, lambdas etc.

  • Does this answer your question? [Sort nested lists by the first element in lists](https://stackoverflow.com/questions/56875302/sort-nested-lists-by-the-first-element-in-lists) – ti7 Aug 04 '21 at 17:48
  • https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – ti7 Aug 04 '21 at 17:53
  • `sort()` isn't a library, it's a `list` method and lists are built-in. If that's still not OK, you should include your code that can do simple sorting in your question. – martineau Aug 04 '21 at 18:54

1 Answers1

0

Just pull your data in by-lines to some structure, splitting on ,

Then you can sort the structure using the key argument to sort()

lines = []
with open("source.csv") as fh:
    for line in fh:  # file-likes are iterable by-lines
        lines.append(line.split(","))

lines.sort(key=lambda x: x[2])  # sort on the 3rd field, (index 2)

If you just wanted the sum, simply keep adding the 3rd field, there's no need to sort it

ti7
  • 16,375
  • 6
  • 40
  • 68