0

I have a list with dictionaries as below, I want to sort data based on 'd' as string in sorting order.

src_dict_list=[{'a':'a1','b':'b1','c':'c1','d':'1000-22'}, 
               {'a':'a1','b':'b1','c':'c1','d':'1000-18'},
               {'a':'a1','b':'b1','c':'c1','d':'1000-11144'},
               {'a':'a1','b':'b1','c':'c1','d':'1000-11146'},
               {'a':'a1','b':'b1','c':'c1','d':'1000-11149'},
               {'a':'a1','b':'b1','c':'c1','d':'1000-11148'}]

here d values are considered as string so I tried with len but I want data to sorted as per numeric values as well.

Code:

print(sorted(src_dict_list,key=lambda k:len(k['d'])))

Output:

[{'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-22'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-18'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11144'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11146'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11149'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11148'}]

Expected:

[{'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-18'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-22'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11144'}, 
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11146'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11148'},
 {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': '1000-11149'}]

can you please help me?

azro
  • 53,056
  • 7
  • 34
  • 70
Vijay
  • 75
  • 4

3 Answers3

1

It sounds like you want to sort based on the lexical ordering of the integers in the string of d. Something like:

sorter = lambda k : list(map(int,k['d'].split('-')))
print(sorted(src_dict_list,key=sorter))
Bobby Ocean
  • 3,120
  • 1
  • 8
  • 15
  • But 'd' value might come in different format also like '1000-00-18','1000-0100-00-18', with out splitting the data, is there any other generic way? – Vijay May 07 '20 at 07:59
  • I am not following how you want them sorted. Do you want the '1000-00-18' first or the '1000-0100-00-18' first? You have some idea of sorting you want, but your not telling us. What are your rules for sorting? – Bobby Ocean May 07 '20 at 08:03
  • my requirement is 2 ways, 1) if it is integer we can directly convert it to int and sort it dictly. we are aware of this. 2) if it is a string having some special characters in 'd' like '-','*' etc, but data should be sorted like integers only. – Vijay May 07 '20 at 08:12
  • Do you mean you want to remove the '-' and '*'? That is int(k['d'].replace('-','').replace('*','')) and make it one big integer? – Bobby Ocean May 07 '20 at 08:21
  • In simply do this. sorter = lambda k : list(map(int,k['d'].split('-'))) print(sorted(src_dict_list,key=sorter)) – Hashan Malawana May 08 '20 at 18:56
0

I'll guess that you want your key function to be something like:

lambda k: tuple(map(int, k['d'].split('-')))

This turns "1000-18" into (1000, 18), i.e. a tuple of ints. That will sort by the numeric value of 1000 primarily and on equality by 18 secondarily.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

You could sort by both the 2 values of d, so first by the 1000 here, then for each 1000, by the second value. For multiple criterias you need to provide a tuple/list in the key, for 1000-18 we would need (1000, 18) (as int values)

So for one value you'd like to operate tuple(map(int,k['d'].split('-')))

Giving

sorted(src_dict_list, key=lambda k:tuple(map(int,k['d'].split('-'))))
azro
  • 53,056
  • 7
  • 34
  • 70