While prototyping in Python, I relied on lists of lists and the operator class's .itemgetter method to do the sorting. The idea behind this is that Python would sort lists within lists based on the index specified.<br><br>While I am absolutely terrible at wrapping my head around things like Big O Notation, I did type out this straightforward bit of code to sort lists of lists. <br><br>The lists I am trying to sort are in the below format and it is assumed that you are sorting by the 1st index as suggested by the magic number within the code. This magic number simply needs to be changed to sort by another index. In this example, the only other option would be the 0th index, which would then sort the strings.<br><br>[ [string,int] , [string,int] , [string,int] ] etc.<br><br>************************************THE CODE************************************<br><br>
var sorted_parent_list = [] # empty until the code runs<br>var parent_list = (list formatted as above)<br>func sort_listed_lists():<br> var sorted_values_of_index_1 = []
<br>#The following loop takes all of the index 1 values and sorts them, putting them in<br>#In this example, the list called sorted_values_of_index_1, as above.<br> for child_list in parent_list:<br> sorted_values_of_index_1.append(child_list[1]) # The magic number<br> sorted_values_of_index_1.sort()<br>#The following loop takes all of the items in the list created in the function above<br>#sorted_values_of_index_1<br>#A nested loop then looks at all of the child lists within the main unsorted parent list, or 'original list of lists'<br>#If the integer from the sorted integers matches the integer in the original list<br>#That list containing the matching integer is added to the sorted_parent_list<br>for sorted_integer in sorted_values_of_index_1:<br>tab>for list in parent_list:<br>tab>tab>if sorted_integer == list[1]:<br>tab>tab>tab>sorted_parent_list.append([list[0],sorted_integer])<br>tab>sorted_values_of_index_1.remove(sorted_integer)<br>print(sorted_parent_list)