a program that asks user to enter number of people their birth year date and month and sorts it
i got the algorithm figured out its just the code thats not hitting me!

Recommended Answers

All 6 Replies

just write down some code and then try to enhance it step by step. then come back here and ask your questions.

what is yor algorithm or logic?

ask the user-for year month and date
if the year is greater than the succeding one it will swap it
once this step is done we set the new array according to year
then we check if the year is same or not
if its same we apply the same method to sort the month annd sort it out
then again we set the new arry according to month and year
after this we check if the year and month is same or not
if it is we check the date and sort it
the final array is then printed out

You want to create a list of [name, year, day. month] lists.

and then initiate the sort/among them?

Start with some code for the

ask the user-for year month and date

part, you need the input() function (in python 3) or raw_input() (in python 2).

Sorting lists is easy in Python:

'''sort_list_lists1.py
sort a list of lists by different value index in the lists
'''

# test list of [name, year, month, day] lists
mylist = [
['Bob', 1994, 11, 12],
['Zoe', 1992, 3, 21],
['Amy', 1993, 5, 16]
]

print("Unsorted:")
print(mylist)

# sort by name
mylist_name = sorted(mylist)

# default sort is by index 0 here name
print("sorted by name:")
print(mylist_name)

# year is at index 1 (lists start with index 0)
mylist_year = sorted(mylist, key=lambda q: q[1])

print("sorted by year:")
print(mylist_year)

'''my result >>>
Unsorted:
[['Bob', 1994, 11, 12], ['Zoe', 1992, 3, 21], ['Amy', 1993, 5, 16]]
sorted by name:
[['Amy', 1993, 5, 16], ['Bob', 1994, 11, 12], ['Zoe', 1992, 3, 21]]
sorted by year:
[['Zoe', 1992, 3, 21], ['Amy', 1993, 5, 16], ['Bob', 1994, 11, 12]]
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.