Total Pageviews

2016/04/05

[Python] How to do sorting in Python

Assume I have a Player Class as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
'''
Created on 2016/2/3

@author: albert
'''
class Player:
    def __init__(self, team, name, position):
        self.Team = team
        self.Name = name
        self.Position = position
    
    def ToString(self):
        return 'team:' + self.Team + ', name:' + self.Name + ', position:' + self.Position


Here has the code snippet which include add players into list, print players before sort, print players after sorted by player's position and name:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from com.cht.Player import *
from operator import attrgetter

''' defined a list of Players'''
players = [
           Player('Lamigo', '陳 金 鋒', '指定代打'),
           Player('Lamigo', '林 柏 佑', '投手'),
           Player('中信兄弟', '彭 政 閔', '一壘手'),
           Player('統一7-ELEVEn', '高 志 綱', '捕手'),
           Player('義大犀牛', '林 晨 樺', '投手'),
           Player('統一7-ELEVEn', '陳 鏞 基', '游擊手'),
           Player('Lamigo', '王 柏 融', '左外野手'),
           Player('義大犀牛', '胡 金 龍', '指定打擊'),
           Player('統一7-ELEVEn', '王 鏡 銘', '投手')
           ]

''' print the list of players before sorted'''
print('\n [before sorted] ')
for player in players:
    print(player.ToString())

''' sort players and position and name '''
sortedPlayers = sorted(players, key=attrgetter('Position', 'Name'), reverse=False)
print('\n [after sorted] ')

''' print the list of players after sorted'''
for player in sortedPlayers:
    print(player.ToString())

The log will print as following:
 [before sorted] 
team:Lamigo, name:陳 金 鋒, position:指定代打
team:Lamigo, name:林 柏 佑, position:投手
team:中信兄弟, name:彭 政 閔, position:一壘手
team:統一7-ELEVEn, name:高 志 綱, position:捕手
team:義大犀牛, name:林 晨 樺, position:投手
team:統一7-ELEVEn, name:陳 鏞 基, position:游擊手
team:Lamigo, name:王 柏 融, position:左外野手
team:義大犀牛, name:胡 金 龍, position:指定打擊
team:統一7-ELEVEn, name:王 鏡 銘, position:投手

 [after sorted] 
team:中信兄弟, name:彭 政 閔, position:一壘手
team:Lamigo, name:王 柏 融, position:左外野手
team:義大犀牛, name:林 晨 樺, position:投手
team:Lamigo, name:林 柏 佑, position:投手
team:統一7-ELEVEn, name:王 鏡 銘, position:投手
team:Lamigo, name:陳 金 鋒, position:指定代打
team:義大犀牛, name:胡 金 龍, position:指定打擊
team:統一7-ELEVEn, name:高 志 綱, position:捕手
team:統一7-ELEVEn, name:陳 鏞 基, position:游擊手


Assume I have a number array, string array, datetime array, I would like to sort by ascendant and descendant. 

Here is the code snippets for utility class:
1
2
3
4
5
6
7
''' reverse=False means sort by ascendant '''        
def sortArrayByAsc(dataArray):
    return sorted(dataArray, reverse=False)

''' reverse=True means sort by descendant '''
def sortArrayByDesc(dataArray):
    return sorted(dataArray, reverse=True)


Test the number array:

1
2
3
4
5
6
7
numberArray = [6, 3, 1, 2, 4, 5]
print('\n Original integer array:')
print(numberArray)
print('Sort integer array by asc:' )
print(utils.sortArrayByAsc(numberArray))
print('Sort integer array by desc:' )
print(utils.sortArrayByDesc(numberArray)) 

Test result:
 Original integer array:
[6, 3, 1, 2, 4, 5]
Sort integer array by asc:
[1, 2, 3, 4, 5, 6]
Sort integer array by desc:
[6, 5, 4, 3, 2, 1]


Test the string array:
nameArray = ['Albert', 'Mandy', 'Fiona', 'Ben', 'Jules']
print('\n Original string array:')
print(nameArray)
print('Sort string array by asc:' )
print(utils.sortArrayByAsc(nameArray))
print('Sort string array by desc:' )
print(utils.sortArrayByDesc(nameArray))

Test result:
 Original string array:
['Albert', 'Mandy', 'Fiona', 'Ben', 'Jules']
Sort string array by asc:
['Albert', 'Ben', 'Fiona', 'Jules', 'Mandy']
Sort string array by desc:
['Mandy', 'Jules', 'Fiona', 'Ben', 'Albert']


Test the datetime array:
import datetime
datetime1 = datetime.datetime.strptime('2015-11-11 10:10:10', '%Y-%m-%d %H:%M:%S')
datetime2 = datetime.datetime.strptime('2014-01-11 12:00:00', '%Y-%m-%d %H:%M:%S')
datetime3 = datetime.datetime.strptime('2016-02-17 11:22:00', '%Y-%m-%d %H:%M:%S')
datetimes = [datetime1, datetime2, datetime3]
print('\n Original datetime array:')
print(datetimes)
print('Sort datetime array by asc:' )
print(utils.sortArrayByAsc(datetimes))
print('Sort datetime array by desc:' )
print(utils.sortArrayByDesc(datetimes))

Test result:
 Original datetime array:
[datetime.datetime(2015, 11, 11, 10, 10, 10), datetime.datetime(2014, 1, 11, 12, 0), datetime.datetime(2016, 2, 17, 11, 22)]
Sort datetime array by asc:
[datetime.datetime(2014, 1, 11, 12, 0), datetime.datetime(2015, 11, 11, 10, 10, 10), datetime.datetime(2016, 2, 17, 11, 22)]
Sort datetime array by desc:
[datetime.datetime(2016, 2, 17, 11, 22), datetime.datetime(2015, 11, 11, 10, 10, 10), datetime.datetime(2014, 1, 11, 12, 0)]


Test the unique list:
names = ['Albert', 'Mandy', 'Mandy', 'Fiona', 'Ben', 'Jules']
print('\n Original name array:')
print(names)
print('Unique name array:')
print(utils.removeDuplicateInList(names))

Test result:
 Original name array:
['Albert', 'Mandy', 'Mandy', 'Fiona', 'Ben', 'Jules']
Unique name array:
['Fiona', 'Ben', 'Mandy', 'Jules', 'Albert']




No comments: