# Golang Program to Sort the 2D Array Across Columns

## What is 2D array in Golang?

In Go programming language, a 2D array is an array of arrays, where each element of the outer array is a simple array itself. The code to declare a 2d array of size 2 X 3 can be done by writing var array . It's important to note that 2D arrays have a fixed size, and once they are created, their size cannot be changed. If you need a dynamic 2D array, you can use a slice of slices.

Here is an example of sorting a 2d array across columns −

### Input

3	2	1
6	4	5
8	9	7


### Output

1	2	3
4	5	6
7	8	9


## Method 1: Using For Loops

In this method, we will be using the “for” loop in the golang program that will help us to sort the 2D array across columns.

### Algorithm

• Step 1 − First, we need to import the fmt package.

• Step 2 − Then, start the main() function. Inside the main() initialize a 2D array of integers having the elements to be sorted. Print the array on the screen using for loop and fmt.Println() function.

• Step 3 − To sort the elements use three for loops within one another. The first two for loops are used to iterate over the 2D array while the third for loops is used to select a particular element of the loop and place it at respective position.

• Step 4 − if the current element of the array is greater than the next element then we need to switch the position of the elements otherwise we have to continue the loop.

• Step 5 − Once every element of the loop is iterated upon the resultant array that we receive is sorted in ascending order. We can print the elements of these arrays now on the screen using fmt.Println() function.

### Example

Following is a Golang program to sort a 2d array across column using a “for” loop

package main
import (
"fmt"
)

func main() {
// 2D array to sort
arr := [][]int{{3, 2, 1}, {6, 4, 5}, {8, 9, 7}}
var rows int = len(arr)
var cols int = len(arr)
fmt.Println("The given 2D array to be sorted is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
// sorting the 2D array
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[i])-1; j++ {
for k := 0; k < len(arr[i])-j-1; k++ {
if arr[i][k] > arr[i][k+1] {
temp := arr[i][k]
arr[i][k] = arr[i][k+1]
arr[i][k+1] = temp
}
}
}
}
fmt.Println()
// printing the sorted 2D array
fmt.Println("The final array obtained after sorting the 2D array is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
}


### Output

The given 2D array to be sorted is:
3	2	1
6	4	5
8	9	7

The final array obtained after sorting the 2D array is:
1	2	3
4	5	6
7	8	9


## Method 2: Using Internal Function

In this method, we will use an internal function to sort the elements of the 2D array.

### Syntax

func len(v Type) int


The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func Ints(slc []int)


Ints() function is present in sorts package and is used to sort a particular array of integers. The function accepts the slice to be sorted as an argument and returns the final slice after sorting the elements of the slice.

type Slice []int


The Slice() function is present in sort package. this function is used to attach the method of interface to the array of int so that it could be sorted in increasing order.

### Algorithm

• Step 1 − First, we need to import the fmt package.

• Step 2 − Then, start the main() function. Inside the main() initialize a 2D array of integers having the elements to be sorted. Print the array on the screen using for loop and fmt.Println() function.

• Step 3 − To sort the elements use a for loop. The for loop is used to iterate over the 2D array and in each iteration pass the element in the Ints() function present in the sort package.

• Step 4 − Once every element of the loop is iterated upon the resultant array that we receive is sorted in ascending order across columns. We can print the elements of these arrays now on the screen using fmt.Println() function.

### Example 1

In the following example we are using sort.Ints() to Sort the 2D Array Across Columns in go programming

package main
import (
"fmt"
"sort"
)
func main() {
// 2D array to sort
arr := [][]int{{3, 2, 1}, {6, 4, 5}, {8, 9, 7}}
var rows int = len(arr)
var cols int = len(arr)
fmt.Println("The given 2D array to be sorted is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
// sorting the 2D array
for i := 0; i < len(arr); i++ {
sort.Ints(arr[i])
}
fmt.Println()
// printing the sorted 2D array
fmt.Println("The final sorted array is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
}


### Output

The given 2D array to be sorted is:
3	2	1
6	4	5
8	9	7

The final sorted array is:
1	2	3
4	5	6
7	8	9


### Example 2

In this method, we are using sort.Slice()function from the slice library function to sort the 2D array across column elements

package main
import (
"fmt"
"sort"
)

type byColumn [][]int

func (a byColumn) Len() int {
return len(a)
}

func (a byColumn) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

func (a byColumn) Less(i, j int) bool {
return a[i] < a[j]
}

func main() {
arr := [][]int{{3, 2, 1}, {7, 9, 8}, {4, 6, 5}}
var rows int = len(arr)
var cols int = len(arr)
fmt.Println("The given 2D array to be sorted is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
sort.Sort(byColumn(arr))
fmt.Println()
fmt.Println("The given 2D array to be sorted is:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
}


### Output

The given 2D array to be sorted is:
3	2	1
7	9	8
4	6	5

The given 2D array to be sorted is:
3	2	1
4	6	5
7	9	8


## Conclusion

We have successfully compiled and executed a go language program to sort the 2D array across columns using the main() and internal library functions.

Updated on: 16-Feb-2023

591 Views 