Javatpoint Logo
Javatpoint Logo

Go Sorting

Go has sort package which can be used for sorting the built-in as well as user defined data types.

The sort package has different methods to sort different data types like Ints(), Float64s(), Strings() etc.

We can check if the values are sort or not by using the AreSorted() methods like Float64sAreSorted(), IntsAreSorted() etc.

Go Sort Example

Output:

Ints:    [5 8 10 20]
floatValue:    [5.5 8.5 10.5 20.5]
Strings: [Mohan Raj Roy]
Sorted:  true

We can also implement our own sorting schema, suppose we want to sort an array of string according to its length. In order to do that we have to implement our own Less, Len and Swap methods defined in the sort Interface.

Then we have to cast our array into the implemented type.

Output:

[Washington New York London Delhi]

Next TopicGo JSON





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA