Python Forum
Sorting lists prioritizing letters before numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting lists prioritizing letters before numbers
#1
Hi guys,

I'm a very new coder so your help means a lot.

I'm trying to sort a list which unsorted might look like this:

001 123
001 Beta 123
402 123
403 123
402 CxR 123
192 UqS 123
001 Alpha 123
309 223
001 038
007 Bond 123

I want to sort this list so that the first three characters are sorted by number, but after that letters are prioritized in sorting. Thus i want the code to return the list sorted like so:

001 Alpha 123
001 Beta 123
001 038
001 123
007 Bond 123
192 UqS 123
309 223
402 CxR 123
402 123
403 123

How do i sort a list like so?

Thanks, guys.
Reply
#2
Ratherfastmofo Wrote:i want the code to return the list sorted like so
Please post the code to show what it currently returns.
Reply
#3
(Nov-21-2018, 09:43 AM)Gribouillis Wrote:
Ratherfastmofo Wrote:i want the code to return the list sorted like so
Please post the code to show what it currently returns.

Currently I've just used the sorted() function to sort a list in the usual pattern. I just want to hear weather there is a way to customize the sorting priorities in the given pattern.
Reply
#4
You'll need to write a custom key function.
You can learn more in the Sorting HOW TO.
Reply
#5
This key function should help.
Maybe it can be optimized.

def key(text):
    offset = ord('z') + 1
    # getting the first 3 chars and convert it to int
    result = [int(text[:3])]
    # iterate over the rest. Whitespace is excluded
    for char in text[4:]:
        if char.isdigit():
            # Add 122 to integer to get a lower prio
            result.append(int(char) + offset)
        else:
            # otherwise just return the ord of char
            result.append(ord(char))
    # result is a list
    return result
The sort function works as follows:

For each element, the key-function is called with this element.
The returned value is a list. A list is sorted by first element, then by second an so on..
The key function just modifies the values for integer, but not for normal letters and other symbols.
I guess it can be optimized. This is just the first shoot.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Split the list into two lists, one containing the alpha lines and one containing the number lines. Sort each one. Merge the number list to the alpha list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with sorting lists as a beginner Realist1c 1 738 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  sorting a list of lists by an element leapcfm 3 1,860 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 2,034 Sep-24-2020, 01:47 PM
Last Post: Drone4four
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,368 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sorting list of lists pframe 5 22,893 Apr-17-2020, 09:31 PM
Last Post: Larz60+
  Sorting numbers from smallest to biggest Dokugan 2 2,217 Apr-14-2020, 09:24 PM
Last Post: Larz60+
  Generate unique random numbers from different lists Takeshio 5 3,769 May-24-2019, 07:29 PM
Last Post: ichabod801
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,720 May-09-2019, 12:19 PM
Last Post: Pleiades
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,044 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,262 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020