3

I have a list as

a = [["1", "ok", "na"], ["15", "asd", "asdasd"], ["100", "uhu", "plo"], ["10", "iju", "tlo"], ["ISC_1", "des", "det"], ["12", "asd", "assrg"], ["ARF", "asd", "rf"]]

I want this list to be sorted as below:

[['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['100', 'uhu', 'plo'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']]

I have used a.sort()

It is resulting as below:

[['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['100', 'uhu', 'plo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']]

Please help me how to sort in this case.

Shanky
  • 203
  • 1
  • 4
  • 15
  • https://stackoverflow.com/questions/20099669/python-sort-multidimensional-array-based-on-2nd-element-of-subarray possibly answer your question – augustoccesar Oct 27 '17 at 13:06
  • Possible duplicate of [Python Sort Multidimensional Array Based on 2nd Element of Subarray](https://stackoverflow.com/questions/20099669/python-sort-multidimensional-array-based-on-2nd-element-of-subarray) – augustoccesar Oct 27 '17 at 13:06

3 Answers3

6

You can use the key named argument.
It accepts a function that returns the value the sorting function should compare items by.

sorted(a, key = lambda l: int(l[0]))
Neo
  • 3,534
  • 2
  • 20
  • 32
  • Will it work in python2.7 ? getting below error `>>> sort(a, key = lambda l: int(l[0])) Traceback (most recent call last): File "", line 1, in sort(a, key = lambda l: int(l[0])) NameError: name 'sort' is not defined` – Sandeep Lade Oct 27 '17 at 13:11
  • with ´a.sort(key = lambda l: int(l[0]))´ or ´b=sorted(a,key = lambda l: int(l[0]))´ – Leonhard Triendl Oct 27 '17 at 13:12
  • what if the case my list is a = [["1", "ok", "na"], ["15", "asd", "asdasd"], ["100", "uhu", "plo"], ["10", "iju", "tlo"], ["ISC_1", "des", "det"]] – Shanky Oct 27 '17 at 13:14
  • It is throwing invalid literal for int() with base 10: 'ISC_1' – Shanky Oct 27 '17 at 13:15
  • 2
    what do you want do happen with ISC? should it come last or first? if it should come last: a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 99999) – Leonhard Triendl Oct 27 '17 at 13:21
  • Thanks for the prompt response Leonhard Triendl Yes ISC_1 should come last. Here another question is if list has oe more alphanumeric as "AFC_1" then it should be "AFC_1" and then "ISC_1" as follows "[['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['15', 'asd', 'asdasd'], ['100', 'uhu', 'plo'], , ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']]" – Shanky Oct 27 '17 at 13:25
  • I will Update my question – Shanky Oct 27 '17 at 13:27
  • @Shanky I have provided an alternative which may cover more bases for you – ragardner Oct 27 '17 at 16:11
3

To be ready for non numeric values you can use

a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 99999)
# or
b=sorted(a,key = lambda l: int(l[0]) if l[0].isnumeric() else 99999)

to see non-numeric last or

a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 0)
# or
b=sorted(a,key = lambda l: int(l[0]) if l[0].isnumeric() else 0)

to see them first

Leonhard Triendl
  • 812
  • 7
  • 16
  • Thanks for the response. I want the result as [['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['100', 'uhu', 'plo'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']] With ARF first and ISC_1 last – Shanky Oct 27 '17 at 13:34
3

You can use a natural sorting key, very easy to setup with the regular expression re.split()

import re
try:
    # fast string checking and conversion
    from fastnumbers import *
except:
    pass

def natural_sort_key_for_list_of_lists(sublist):
    return [int(element) if element.isdigit() else element
            for element in re.split("([0-9]+)",sublist[0])]
    # put whichever index of the sublist you want here ^

a = [["1", "ok", "na"],
     ["15", "asd", "asdasd"],
     ["100", "uhu", "plo"],
     ["10", "iju", "tlo"],
     ["ISC_1", "des", "det"],
     ["12", "asd", "assrg"],
     ["ARF", "asd", "rf"]]

a.sort(key=natural_sort_key_for_list_of_lists)

for l in a:
    print (l)

result:

['1', 'ok', 'na']
['10', 'iju', 'tlo']
['12', 'asd', 'assrg']
['15', 'asd', 'asdasd']
['100', 'uhu', 'plo']
['ARF', 'asd', 'rf']
['ISC_1', 'des', 'det']
ragardner
  • 1,836
  • 5
  • 22
  • 45