python sorting date without using build in function sort() , but the expect i want is
['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-11-16',
'2010-11-22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13',
'2011-02-04', '2011-06-02', '2011-08-05', '2011-11-30']
I did read the similar problem and try to sloved it but still stock
How to sort things, without using the sorted build-in function?
first version code:
import datetime
def date_sorting_operation(input_list):
input_list = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list]
for i in range(len(input_list)):
for j in range(i + 1, len(input_list)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
return input_list
customer_date_list = ['2011-06-02', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']
print (date_sorting_operation(customer_date_list))
second version code:
import datetime
def date_sorting_operation(input_list):
input_list = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list]
for i in range(len(input_list) - 1):
for j in range(0, len(input_list) - i - 1):
if input_list[j].repeats > input_list[j + 1].repeats:
input_list[j], input_list[j + 1] = input_list[j + 1], input_list[j]
return input_list
customer_date_list = ['2011-06-02', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']
print (date_sorting_operation(customer_date_list))
(pic) for code and output First version:

(pic) for code and output Second version:

(Third version) Try just to use "input_list", remove "new_list" to save memory if that, how can I do so?
import datetime
def date_sorting_operation(input_list):
new_list = []
dates = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list]
while input_list:
min = input_list[0]
for x in input_list:
if x < min:
min = x
new_list.append(min)
input_list.remove(min)
return new_list
customer_date_list = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']
print (date_sorting_operation(customer_date_list))
(pic) try just to use "input_list", remove "new_list" to save memory:
