19

I have a list of lines read from a file. I need to sort the list by time stamp. I have parsed out the time stamp using regular expressions and place them into a separate list. The indices of the two lists will match. Once I sort the list of time stamps, I can get the order of indices.

Is there a way to apply the same order of indices to the original list of lines? The result should be the sorted list of original lines.

Example:

listofLines =  ['log opened 16-Feb-2010 06:37:56 UTC', 
                '06:37:58 Custom parameters are in use',
                'log closed 16-Feb-2010 05:26:47 UTC']
listofTimes = ['06:37:56', '06:37:58', '05:26:47']
sortedIndex = [2,0,1]
Seanny123
  • 8,776
  • 13
  • 68
  • 124
Hannah
  • 2,805
  • 4
  • 19
  • 10
  • Rather than do extra work to figure out the indices and then apply them back to the other list, why not pair the values up before sorting (for example, using `zip`)? – Karl Knechtel Sep 13 '22 at 18:48

4 Answers4

50
[listofLines[i] for i in sortedIndex]
sepp2k
  • 363,768
  • 54
  • 674
  • 675
11

I think you could do

[line for (time,line) in sorted(zip(listofTimes, listofLines))]

But if you have (or could write) a function to automatically extract the time from the line,

def extract_time(line):
    ...
    return time

you could also do

listofLines.sort(key=extract_time)

or if you want to keep the original list intact,

sorted(listofLines, key=extract_time)
David Z
  • 128,184
  • 27
  • 255
  • 279
3
sorted(zip(listofTimes, listofLines))
Anycorn
  • 50,217
  • 42
  • 167
  • 261
1

If you want to sort the original list because you, say, hold references to it elsewhere, you can assign to it the sorted list:

my_list[:] = [my_list[i] for i in sorted_indexes]  # [:] is key!
K3---rnc
  • 6,717
  • 3
  • 31
  • 46