-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.Sort 2d slice.go
53 lines (47 loc) · 894 Bytes
/
6.Sort 2d slice.go
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
package main
import (
"bufio"
//"encoding/hex"
"fmt"
"os"
"sort"
"strconv"
)
type Matrix [][]int
func (m Matrix)Len()int {return len(m)}
func (m Matrix)Less(i,j int) bool{
//for x:= range m[i] {
// if m[i][x] == m[j][x] {
// continue
// }
// return m[i][x]<m[j][x]
//}
//return false
return m[i][1]<m[j][1]
}
func (m Matrix)Swap(i,j int ){
m[i],m[j]=m[j],m[i]
}
func main (){
fmt.Println("### 2d slice of any type ####")
fmt.Println("n x m slice ...")
fmt.Printf("value n : ")
var n,m int
fmt.Scanln(&n)
fmt.Printf("value m : ")
fmt.Scanln(&m)
fmt.Println("give the values according to row : ")
sli := make(Matrix,0)
for i:=0;i<n;i++{
li:=make([]int,0)
for j:=0;j<m;j++{
x,_,_ :=bufio.NewReader(os.Stdin).ReadLine()
ans,_:=strconv.Atoi(string(x))
li=append(li,ans)
}
sli=append(sli,li)
}
fmt.Println(sli)
sort.Sort(sli)
fmt.Println(sli)
}