0

I'm a beginner in Python, I was wondering how do I sort specific data in a file.

For example, if I want to sort and display a record of Coaches Hourly Pay Rate in ascending order.

I only managed to sort the coach name and was unable to sort other categories in the file.

The ideal output will be like the text file but in ascending order of the category I sort.

Coach.txt
Coach name:Adam Coach ID:001 Sport:Swimming Hourly Wages:RM100 Coach Overall Performance:4,  
Coach name:Jennie Coach ID:002 Sport:Badminton Hourly Wages:RM200 Coach Overall Performance:5,  
Coach name:Roger Coach ID:003 Sport:Football Hourly Wages:RM150 Coach Overall Performance:4,  
Coach name:Artemis Coach ID:004 Sport:Archery Hourly Wages:RM300 Coach Overall Performance:5,  
Coach name:Rose Coach ID:005 Sport:Gymnastics Hourly Wages:RM300 Coach Overall Performance:5,  
Coach name:Brandon Coach ID:006 Sport:Volleyball Hourly Wages:RM100 Coach Overall Performance:4,  
Coach name:Kobe Coach ID:007 Sport:Basketball Hourly Wages:RM500 Coach Overall Performance:5,  
Coach name:Muthu Coach ID:008 Sport:Cricket Hourly Wages:RM100 Coach Overall Performance:3,  
Coach name:Chong Coach ID:009 Sport:Tennis Hourly Wages:Rm150 Coach Overall Performance:4,  
Coach name:Armin Coach ID:010 Sport:Tabble Tennis Hourly Wages:RM150 Coach Overall Performance:5,  
f = open("coach.txt", "r")
payrate = []
i = 0
for line in f:
        split = line.split(",")
        payrate.append(split[0].rstrip())
        for i in range(0, len(payrate)):
            for j in range(i+1, len(payrate)):
                if (payrate[i]) > (payrate[j]):
                    temp = payrate[i]
                    payrate[i] = payrate[j]
                    payrate[j] = temp

print("The coach records by ascending order of name:\n", payrate)

f.close()
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Help SOS
  • 3
  • 5
  • Welcome to Stack Overflow! Do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/) for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Sabito stands with Ukraine Jan 17 '21 at 04:36
  • @yatin i would like to delete this question – Help SOS Jan 20 '21 at 08:48
  • There should be a delete button... – Sabito stands with Ukraine Jan 20 '21 at 08:54

2 Answers2

0

Try run this:

f = open("coach.txt", "r")
payrate = []
payNames = []
payFinal = []
i = 0
for line in f:  
  if line.lower().find(":rm") != -1 : 
    nameStart = line.lower().split("coach name:")
    nameEnds = nameStart[1].lower().split(" coach id:")
    crntName = nameEnds[0] 
     
    startFrom = line.lower().split(":rm") 
    endsTo= startFrom[1].lower().split(" coach overall") 
    crntRate = int(endsTo[0])  
    i += 1

    payFinal.append([[crntRate,crntName, line]])
  


payFinal.sort(key=lambda x: x[0])
print("The coach records by ascending order of name:\n", payFinal)

f.close()

Results:

[[[100, 'brandon', 'Coach name:Brandon Coach ID:006 Sport:Volleyball Hourly Wages:RM100 Coach Overall \n']], [[100, 'muthu', 'Coach name:Muthu Coach ID:008 Sport:Cricket Hourly Wages:RM100 Coach Overall \n']], [[150, 'armin', 'Coach name:Armin Coach ID:010 Sport:Tabble Tennis Hourly Wages:RM150 Coach Overall \n']], [[150, 'chong', 'Coach name:Chong Coach ID:009 Sport:Tennis Hourly Wages:Rm150 Coach Overall \n']], [[150, 'roger', 'Coach name:Roger Coach ID:003 Sport:Football Hourly Wages:RM150 Coach Overall \n']], [[200, 'jennie', 'Coach name:Jennie Coach ID:002 Sport:Badminton Hourly Wages:RM200 Coach Overall \n']], [[300, 'artemis', 'Coach name:Artemis Coach ID:004 Sport:Archery Hourly Wages:RM300 Coach Overall \n']], [[300, 'rose', 'Coach name:Rose Coach ID:005 Sport:Gymnastics Hourly Wages:RM300 Coach Overall \n']], [[500, 'kobe', 'Coach name:Kobe Coach ID:007 Sport:Basketball Hourly Wages:RM500 Coach Overall \n']]] [28]

al_qaysee
  • 81
  • 1
  • 6
  • can u do a code that's without the build in function? Thank you – Help SOS Jan 16 '21 at 15:44
  • payFinal.sort .. means sort the array ascending according to the column number 0, which is the RM value in your case. SEE: https://stackoverflow.com/questions/2173797/how-to-sort-2d-array-by-row-in-python – al_qaysee Jan 16 '21 at 15:44
  • check my new answer ! has a loop without sort function. – al_qaysee Jan 16 '21 at 19:06
0

Here is another one with looping only!

f = open("coach.txt", "r")
payrate = []
payNames = []
payFinaltemp = []
i = 0
for line in f:  
  if line.lower().find(":rm") != -1 : 
    nameStart = line.lower().split("coach name:")
    nameEnds = nameStart[1].lower().split(" coach id:")
    crntName = nameEnds[0] 
     
    startFrom = line.lower().split(":rm") 
    endsTo= startFrom[1].lower().split(" coach overall") 
    crntRate = int(endsTo[0])  
    i += 1

    payFinaltemp.append([[crntRate,crntName, line]])
  

 

def sortArrays(fullArr):
  arr = []
  for i in range(len(fullArr)):
      arr.append(fullArr[i][0][0])
  
  length = len(arr) 
  j = 0
  while j < length - 1:
    if (arr[j] > arr[j + 1]): 
      temp = arr[j] 
      arr[j] = arr[j + 1] 
      arr[j + 1] = temp  
      temp2 = payFinaltemp[j][0]
      payFinaltemp[j][0] = payFinaltemp[j+1][0]
      payFinaltemp[j+1][0] = temp2  
      j = -1
    j += 1
  return payFinaltemp
  

  
 
print("The coach records by ascending order of name:\n", sortArrays(payFinaltemp))

f.close()
al_qaysee
  • 81
  • 1
  • 6