-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortstructs.go
More file actions
executable file
·54 lines (35 loc) · 817 Bytes
/
Copy pathsortstructs.go
File metadata and controls
executable file
·54 lines (35 loc) · 817 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import "os"
import "fmt"
import "strings"
import "strconv"
import "sort"
type player struct {
name string
scores int
}
func main(){
players := []player{}
data,_ := os.ReadFile("sample.txt")
datastring := string(data)
sliceddata:= strings.Split(datastring, "\n")
for _,details := range sliceddata {
parts := strings.Fields(details)
var p player
p.name = parts[0]
scr,_ := strconv.Atoi( parts[1] )
p.scores = scr
players = append(players,p)
}
fmt.Println(players)
sort.Slice(players, func(i, j int) bool {
return players[i].scores > players[j].scores
} )
rank :=1
for a:=0 ; a <= len(players) -1 ; a++{
if a > 0 && players[a].scores != players[a-1].scores {
rank = a + 1
}
fmt.Println( players[a].name ,rank)
}
}