Python Forum
Advanced sorting of a built-in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advanced sorting of a built-in list
#1
Hello everyone, I'm new to the python forum and I need help about ending my python program.

My goal is to sort a built-in list by date of birth of the person from oldest to youngest.

Once this sorting is done, I then add the names and first names in relation of course...

I tried several things, but unfortunately I only managed to do it by using the person's age ("example: 18 years" but I didn't manage to do it for "18/06/1989").

I would like to do this without using a sort function.
So i don't want to reproduce the use of sort function..


The goal is ultimately to make this sorting by calling a function and the return is supposed to be of this type.
WILSON MIKE 26/06/1950
EMERIC JAMES 27/06/1960
MOPAL ARTHUR 27/06/1966



At the moment I have been able to do it only with the age of the form "66" and not "26/06/1966" of the person as well as with the use of sorting...

listepersonnes = [['WILSON', 'MIKE', 'H', '68'], ['EMERIC', 'JAMES', 'H', '58'], ['MOPAL', 'ARTHUR', 'H', '52']]
def agepersonne():
  print(sorted([i[:4] for i in listepersonnes], key=lambda x: x[3], reverse = True))
agepersonne()
Result is :

WILSON MIKE 68
EMERIC JAMES 58
MOPAL ARTHUR 52
My new list of persons should be like this :
listepersonnes = [['WILSON', 'MIKE', 'H', '26/06/1950'], ['EMERIC', 'JAMES', 'H', '27/06/1960'], ['MOPAL', 'ARTHUR', 'H', '27/06/1966']]
Thanks for all !
Reply
#2
You most first check if date string work as date object.
>>> from datetime import datetime
>>> datetime.strptime('26/06/1950', '%d/%m/%Y')
datetime.datetime(1950, 6, 26, 0, 0)
Now can sort bye dates.
>>> lst = [['WILSON', 'MIKE', 'H', '26/06/1950'], ['EMERIC', 'JAMES', 'H', '27/06/1960'], ['MOPAL', 'ARTHUR', 'H', '27/06/1966']]
>>> sorted(lst, key=lambda item: datetime.strptime(item[3], '%d/%m/%Y'), reverse=True)
[['MOPAL', 'ARTHUR', 'H', '27/06/1966'],
 ['EMERIC', 'JAMES', 'H', '27/06/1960'],
 ['WILSON', 'MIKE', 'H', '26/06/1950']]
reverse it will be from oldest to youngest.
The first and last name should maybe not be split up,could be one string 'WILSON MIKE'.
Reply
#3
Thanks for the answer !
Yes, I understand your grouping between name and first name, but I need to separate them to use them in other functions.

But your code use the sort function :)
My wish is not to use the sorting function but rather to do it by hand.
As you can see I already realized the process with the sorting function but this time I want to do it completely by hand.

Thanks for all !
Reply
#4
Hello,
I slightly modified the list
Could you tell me how to sort out the dates ?
L = [['WILSON', 'MIKE', 'H', '19500626'], ['EMERIC', 'JAMES', 'H', '19600626'], ['MOPAL', 'ARTHUR', 'H', '19660627']]
def triInsertion(L):
    for i in range(len(L)):
        if L[i][3] > L[i-1][3]:
            for k in range(0, i):
                if L[i] < L[k]:
                    X = L.pop(i)
                    L.insert(k, X)
                    break
    return L
 
triInsertion(L)
Reply
#5
Moved to homework, as this is clearly homework.

What do your dates look like? Every post has a different format for them, with the last one not really looking like any sort of date at all.
Reply
#6
(Jan-11-2018, 06:03 PM)nilamo Wrote: the last one not really looking like any sort of date at all.
it is YYYYMMDD, or using format string %Y%m%d
Reply
#7
(Jan-11-2018, 06:46 PM)buran Wrote:
(Jan-11-2018, 06:03 PM)nilamo Wrote: the last one not really looking like any sort of date at all.
it is YYYYMMDD, or using format string %Y%m%d

Yes it's that YYYYMMDD
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,355 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sorting list - Homework assigment ranbarr 1 2,219 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,875 May-14-2021, 07:14 AM
Last Post: perfringo
  Advanced Algorithms and Computational Models hafedh 4 2,307 Aug-31-2020, 06:37 PM
Last Post: buran
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,670 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,276 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  sorting a deck of cards (objects in a list) itmustbebunnies 1 7,184 Dec-05-2018, 02:44 AM
Last Post: ichabod801
  Help with list sorting gonzo620 1 3,106 Oct-16-2018, 02:58 PM
Last Post: j.crater
  Sorting list of names by first two characters Otbredbaron 2 3,263 May-24-2018, 03:59 PM
Last Post: Otbredbaron
  Sorting Santa's List of Children sirox84 4 5,148 Feb-20-2017, 06:10 PM
Last Post: sirox84

Forum Jump:

User Panel Messages

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