-2

I have no idea how to proceed with this problem. I have a list containing multiple dictionaries which has some values. I have to sort the based on a value of 'emp_code' in alphanumeric way and emp_code is an alphanumeric value

[{'emp_code':'AB100', 'emp_name':'John'}, {'emp_code':'3', 'emp_name':'Prince'}, {'emp_code':'BA250', 'emp_name':'Jack'},
{'emp_code':'10', 'emp_name':'Jackson'}]

Please help me with this

Expected Output

[{'emp_code':'3', 'emp_name':'Prince'},  {'emp_code':'10', 'emp_name':'Jackson'},
{'emp_code':'AB100', 'emp_name':'John'}, {'emp_code':'BA250', 'emp_name':'Jack'}, ]

I have tried it using sorted function, the sorting of emp_code which is alphanumeric is not working properly. emp_code with 10 is coming before emp_code 3 which is wrong.

  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. Sorting is a well-documented field, with many available tutorials and examples. – Prune Nov 13 '20 at 06:22
  • Hey, not to be rude but on SO, please do an extensive search for your problem before you post a question. Also, when you DO post a question, it is expected that you have at least attempted it from your side. Please refer to the above links that @Prune has mentioned. You will find that this question more or less answers what you need exactly [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Akshay Sehgal Nov 13 '20 at 06:24
  • I have done it...by of no use. I have to sort alphanumeric value...that's my problem – Music 2 News Nov 13 '20 at 06:28
  • You can simply iterate over the list and take the value for each emp code and sort it based on that. Your problem is comprised of 'HOW TO SORT A LIST' and 'HOW TO GET A VALUE FOR A SPECIFIC KEY FROM A DICTIONARY'. This should be easy enough with these 2 things to solve. Do update your answer with an attempt. – Akshay Sehgal Nov 13 '20 at 06:29
  • Could you show me an example of those two please..I got stuck – Music 2 News Nov 13 '20 at 06:32
  • FIrst post an expected output in your question. – Akshay Sehgal Nov 13 '20 at 06:33
  • 2
    Please quit asking for someone to hand you code: Stack Overflow is not a code-writing service. Since you have not yet researched the algorithm nor attempted to code the problem yourself, you do not yet have a Stack Overflow question. – Prune Nov 13 '20 at 06:36
  • Akshay, I have posted expected Output in my question – Music 2 News Nov 13 '20 at 06:38
  • I have posted a solution to help guide for this question, but please be careful of posting such questions in the future. – Akshay Sehgal Nov 13 '20 at 06:40

1 Answers1

1

EDIT: This should solve your problem by sorting the strings as int of base 36.

Base36 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-36 representation. The choice of 36 is convenient in that the digits can be represented using the Arabic numerals 0–9 and the Latin letters A–Z (the ISO basic Latin alphabet).

dd = [{'emp_code':'AB100', 'emp_name':'John'}, 
      {'emp_code':'3', 'emp_name':'Prince'}, 
      {'emp_code':'BA250', 'emp_name':'AC500'}]

sorted(dd, key=lambda x: int(x.get('emp_code'),36))
[{'emp_code': '3', 'emp_name': 'Prince'},
 {'emp_code': 'AB100', 'emp_name': 'John'},
 {'emp_code': 'BA250', 'emp_name': 'AC500'}]

Another example as you mentioned in your comments -

dd = [{'emp_code': '100', 'emp_name': 'Ramesh'},
      {'emp_code': '3', 'emp_name': 'Prince'}]

sorted(dd, key=lambda x: int(x.get('emp_code'), 36))
[{'emp_code': '3', 'emp_name': 'Prince'},
 {'emp_code': '100', 'emp_name': 'Ramesh'}]

Please refrain from posting such questions in the future, else the question will be flagged as poor quality and closed.

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • you have to be more specific than that bro. – Akshay Sehgal Nov 13 '20 at 06:43
  • If I pass a new emp dictionary data..for example This should solve your problem - [{'emp_code': '100', 'emp_name': 'Ramesh'} [{'emp_code': '3', 'emp_name': 'Prince'}, the result is 3 should come first..but it's not happening – Music 2 News Nov 13 '20 at 06:44
  • Is your data structure a `list of dictionaries` or a `list of lists of dictionaries`? – Akshay Sehgal Nov 13 '20 at 06:47
  • List of dictionaries only – Music 2 News Nov 13 '20 at 06:47
  • Sorry that a mistake...it's just a list of dictionaries – Music 2 News Nov 13 '20 at 06:48
  • thats kinda the problem here then. You want to sort based on alphanumeric, but '100' comes before '3' if you sort based on that. – Akshay Sehgal Nov 13 '20 at 06:50
  • Yes...thats what the problem which I am facing..100 is coming before 3 – Music 2 News Nov 13 '20 at 06:51
  • And thats exactly the point. If you want to sort by integer value then that would work, but otherwise an alphanumeric sort will always give you that result. You have to decide what your sorting logic actually does. – Akshay Sehgal Nov 13 '20 at 06:53
  • Is there any way bro...? – Music 2 News Nov 13 '20 at 06:53
  • That's the problem I am nto able to proceed...and got stuck...hope you can help me bro – Music 2 News Nov 13 '20 at 06:56
  • Ill request if you can update your question in that case. Your question is misleading. Please rewrite the question as a problem around a sorting logic instead of the current. That would help your question get more responses and not flagged – Akshay Sehgal Nov 13 '20 at 06:58
  • I think the only way you have to solve this problem is to convert and sort your strings as hexadecimal string. That way you should be able to get the results needed. Converting each of the strings to base 16 integers (hex) and then sorting may solve your problem. – Akshay Sehgal Nov 13 '20 at 07:11
  • note that this will only work for alphabets till F. I dont see any other way of solving your sorting logic problem sorry. – Akshay Sehgal Nov 13 '20 at 07:14
  • Yes...bro...it's not working for character more than F – Music 2 News Nov 13 '20 at 07:19
  • Change it to base 36 i guess. Its a makeshift way to do this. Let me know if that solves your problem. I have updated my answer. Let me know if that fixes it for you. – Akshay Sehgal Nov 13 '20 at 08:07
  • Some what able to get the solution bro...I am giving the outputs what I am getting in the ascending order of only emp_code 13, 100, E13, A300, B900, E100, AB100, XY100, XY900 – Music 2 News Nov 13 '20 at 09:25