Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DisplayOrder:
- def __init__(self, number, description):
- self.number = number
- self.description = description
- def switch1(listToSwitch, source, target):
- temp1 = listToSwitch[source]
- temp2 = listToSwitch[target]
- listToSwitch[source] = temp2
- listToSwitch[target] = temp1
- def sort(listToSort, switch):
- res = listToSort
- for i in range(0, len(res)):
- if (res[i].description == switch and i + 1 < len(res) and res[i].number == res[i + 1].number):
- switch1(res, i, i + 1)
- break
- elif (i > 0 and res[i].number == res[i - 1].number):
- switch1(res, i, i - 1)
- break
- return res
- def smartSort(listToSort, switch):
- sortedList = sort(listToSort, switch)
- for i in range(0, len(sortedList) - 1):
- delta = (sortedList[i].number + 1) - sortedList[i + 1].number
- sortedList[i + 1].number = sortedList[i + 1].number + delta
- return sortedList
- def stupidSort(listToSort, switch):
- sortedList = sort(listToSort, switch)
- for i in range(0, len(sortedList)):
- if (sortedList[i].description != switch):
- sortedList[i].number = i + 1
- return sortedList
- foo = [
- DisplayOrder(1, "a"),
- DisplayOrder(1, "b"),
- DisplayOrder(3, "c"),
- DisplayOrder(4, "d"),
- DisplayOrder(5, "e"),
- DisplayOrder(6, "f"),
- DisplayOrder(7, "g"),
- DisplayOrder(8, "i"),
- ]
- bar = [
- DisplayOrder(1, "a"),
- DisplayOrder(1, "b"),
- DisplayOrder(3, "c"),
- DisplayOrder(4, "d"),
- DisplayOrder(5, "e"),
- DisplayOrder(6, "f"),
- DisplayOrder(7, "g"),
- DisplayOrder(8, "i"),
- ]
- print("smartSort")
- for f in smartSort(foo, "b"):
- print(f.number, f.description)
- print("stupidSort")
- for f in stupidSort(foo, "b"):
- print(f.number, f.description)
Advertisement
Add Comment
Please, Sign In to add comment