Node.js

npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

npm can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. When used as a dependency manager for a local project, npm can install, in one command, all the dependencies of a project through the package.json file. In the package.json file, each dependency can specify a range of valid versions using the semantic versioning scheme, allowing developers to auto-update their packages while at the same time avoiding unwanted breaking changes. npm also provides version-bumping tools for developers to tag their packages with a particular version. npm also provides the package-lock.json file which has the entry of the exact version used by the project after evaluating semantic versioning in package.json.

package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

Golang Sort

To sort in Go, we use the “sort” package. We sort strings alphabetically with Strings(). For complex things, we use Sort and define an interface (with Len, Less and Swap).

Strings. This method implements an ascending (low to high, alphabetical) sort of strings. So they go from A to Z. We must add the “sort” package to our import block.

In-place:The Strings method operates in-place. So we do not need to assign anything to it—it modifies the slice.
Slice:The Strings method, as well as other methods in “sort,” all operate on slices. This is the standard Go approach.
package main

import (
    "fmt"
    "sort"
)

func main() {
    animals := []string{"cat", "bird", "zebra", "fox"}

    // Sort strings.
    sort.Strings(animals)

    // Print results.
    fmt.Println(animals)
}

Sort strings by length. Here we specify how elements in a slice are sorted. We use the “type” keyword to create a type. We implement the sort.Interface on our ByLen type.

Len:This method is required by sort.Interface. It is used by the sort.Sort func.
Less:This compares two elements of the type. Here we use custom code to compare the lengths of two elements.
Swap:This is used by the sorting algorithm to swap two elements in the collection.
package main

import (
    "fmt"
    "sort"
)

// Implement length-based sort with ByLen type.
type ByLen []string
func (a ByLen) Len() int           { return len(a) }
func (a ByLen) Less(i, j int) bool { return len(a[i]) < len(a[j]) }
func (a ByLen) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
    // These elements are not sorted.
    elements := []string{"ruby", "python", "java", "go"}

    // Sort the elements by length.
    sort.Sort(ByLen(elements))

    // Print results.
    fmt.Println(elements)
}

 

Sort keys in map. A map cannot be sorted directly. But if we get a slice of the keys from a map, we can sort that slice and loop over it, accessing the map’s values.Map

Important:In this example, we are not sorting the map, but sorting a slice of the map’s keys. The original map is unchanged.
package main

import (
    "fmt"
    "sort"
)

func main() {
    codes := map[string]int{
        "xyz": 1,
        "ghi": 1,
        "abc": 1,
        "def": 1,
    }

    // Get keys from map.
    keys := []string{}
    for key, _ := range codes {
        keys = append(keys, key)
    }

    // Sort string keys.
    sort.Strings(keys)

    // Loop over sorted key-value pairs.
    for i := range keys {
        key := keys[i]
        value := codes[key]
        fmt.Printf("%v = %v", key, value)
        fmt.Println()
    }
}