SoFunction
Updated on 2025-03-03

golang multi-dimensional sorting and the longest continuous sequence of the problem solution

golang Multi-dimensional sorting

func main() {
    type PersonAge struct {
        Name  string
        Age   int
        Hight int
    }
    ps := []PersonAge{
        {"bo", 31, 333},
        {"ao", 42, 34},
        {"ao", 41, 23423},
        {"ao", 40, 23423},
        {"ao", 45, 12},
        {"co", 17, 3434},
        {"do", 26, 2343},
    }
    (ps, func(i, j int) bool {
        //!= Sort when there is size, when equal, the priority of the next option        if ps[i].Name != ps[j].Name {
            return ps[i].Name < ps[j].Name
        }
        if ps[i].Hight != ps[j].Hight {
            return ps[i].Hight < ps[j].Hight
        }
        return ps[i].Age < ps[j].Age
    })
    (ps)
    return
}

result

[{ao 45 12} {ao 42 34} {ao 40 23423} {ao 41 23423} {bo 31 333} {co 17 3434} {do 26 2343}]

The longest continuous sequence of the problem

Given an unsorted array of integers nums , find out the length of the longest sequence of numbers that are continuous (without requiring sequence elements to be continuous in the original array).

Please design and implement an algorithm with a time complexity of O(n) to solve this problem.

The original question of the longest continuous sequence

Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest continuous sequence of numbers is [1, 2, 3, 4]. It has a length of 4.
Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9

//Sortfunc longestConsecutive(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    (nums)
    long := 1
    lst := 1
    tmp := nums[0]
    for i, _ := range nums {
        if nums[i] == tmp+1 {
            long++
            lst = max(lst, long)
            tmp = nums[i]
        } else if nums[i] == tmp {
            continue
        } else {
            long = 1
            tmp = nums[i]
        }
    }
    return lst
}
func max(a,b int)int{
    if a>b{
        return a
    }
    return b
}

The above is the detailed content of golang multi-dimensional sorting and the longest continuous sequence of the problem solution. For more information about golang sorting, please pay attention to my other related articles!