Alphanumerically sort a nested list of mixed data types in Python

Self_Taught_by_Necessity

Assuming I have the following nested list:

nestedlist = [[ 'ABC' ,  1.5   ,  2  ,  '8W'  ],
              [   2   , 'EXT'  , 5.8 ,  '2W'  ],
              [ 'DEF' ,   2    , 0.2 ,  '2Z'  ]]

for the sake of explaining my question I will refer to the inner lists as rows (i.e nestedlist[0] = row1, nestedlist[1] = row2, etc...) and collectively the items from within the inner lists that share the same ordinal index as columns (i.e. nestedlist[0][0] = item1 of column1, nestedlist[1][0] = item2 of column1, etc...)

How would I sort the rows based on the values within a particular column such that if sorted on column1 the resulting structure would look like the following:

 nestedlist = [[   2   , 'EXT'  , 5.8 ,  '2W'  ],
               [ 'ABC' ,  1.5   ,  2  ,  '8W'  ],
               [ 'DEF' ,   2    , 0.2 ,  '2Z'  ]]

if sorted on column2 the structure would look like:

 nestedlist = [[ 'ABC' ,  1.5   ,  2  ,  '8W'  ],
               [ 'DEF' ,   2    , 0.2 ,  '2Z'  ],
               [   2   , 'EXT'  , 5.8 ,  '2W'  ]]

if sorted on column3 the structure would look like:

 nestedlist = [[ 'DEF' ,   2    , 0.2 ,  '2Z'  ],
               [ 'ABC' ,  1.5   ,  2  ,  '8W'  ],
               [   2   , 'EXT'  , 5.8 ,  '2W'  ]]

and lastly if sorted on column4 the structure would be:

 nestedlist = [[   2   , 'EXT'  , 5.8 ,  '2W'  ],
               [ 'DEF' ,   2    , 0.2 ,  '2Z'  ],
               [ 'ABC' ,  1.5   ,  2  ,  '8W'  ]]

I am aware that if all of the items within a column are of the same type than the sorted(nestedlist, key=itemgetter(sortColIndex)) function can be used but I cannot for the life of me figure out how to get this to work using mixed types.

Self_Taught_by_Necessity

Thank you everyone for the comments and solutions you provided. searching several of the links you provided and the subsequent links there within lead me to http://productarchitect.com/code/better-natural-sort.py which addresses all of the edge cases I was encountering. Thanks again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sort a list of lists in Python with different data types

From Dev

Sort javascript array alphanumerically

From Dev

Mixed types in Scala List

From Dev

Convert list of tuples of mixed data types into all string

From Dev

Convert list of tuples of mixed data types into all string

From Dev

python pandas text block to data frame mixed types

From Dev

Magento Sort Attribute by Decimal not Alphanumerically

From Dev

Magento Sort Attribute by Decimal not Alphanumerically

From Dev

Storing a list of mixed types in Cassandra

From Dev

Conditional operator with mixed data types?

From Dev

Conditional operator with mixed data types?

From Dev

Python nested defaultdict with mix data types

From Dev

Convert a mixed nested list to a nested tuple

From Dev

Creating a mixed python data structure by iterating over a list of lists

From Dev

Python Mixed data structure

From Dev

Underscore.js sort an array of objects alphanumerically

From Dev

How to sort values alphanumerically that have different translations?

From Dev

Sort text column alphanumerically (letters before numbers)

From Dev

Sort a mixed list of headings in org mode

From Dev

Sort list of mixed strings based on digits

From Dev

Sort Nested Markdown List?

From Dev

How to perform constraint solving with mixed data types?

From Dev

Pulling table data with mixed element types

From Dev

Combining mixed data types in pandas column

From Dev

How to handle mixed data types in numpy arrays

From Dev

Javascript sort() array of mixed data type

From Dev

Javascript sort() array of mixed data type

From Dev

Display the Data types of a given list using python

From Dev

How to sort data in the dictionary of list of dictionary in python?

Related Related

HotTag

Archive