Skip to content

语义化版本倒序

语义化版本倒序排列。

示例

输入:['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']

输出:['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1' ]

代码

javascript
// 输入:['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']
// 输出:['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1' ]

export function versionListSort (list) {
  let listArr = list.map(x => x.split('.').map(x => Number(x)))
  const maxLength = Math.max(...listArr.map(x => x.length))
  let sortIndex = 0
  while(sortIndex < maxLength) {
    listArr = listArr.sort((a, b) => {
      const aVal = a[sortIndex] || 0
      const bVal = b[sortIndex] || 0
      if (sortIndex > 0) {
        if (a[sortIndex - 1] === b[sortIndex - 1]) {
          return bVal - aVal
        } else {
          return 0
        }
      }
      return bVal - aVal
    })
    sortIndex++
  }
  return listArr.map(x => x.join('.'))
}
typescript
// 输入:['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']
// 输出:['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1' ]

export function versionListSort (list: string[]): string[] {
  let listArr = list.map(x => x.split('.').map(x => Number(x)))
  const maxLength = Math.max(...listArr.map(x => x.length))
  let sortIndex = 0
  while(sortIndex < maxLength) {
    listArr = listArr.sort((a, b) => {
      const aVal = a[sortIndex] || 0
      const bVal = b[sortIndex] || 0
      if (sortIndex > 0) {
        if (a[sortIndex - 1] === b[sortIndex - 1]) {
          return bVal - aVal
        } else {
          return 0
        }
      }
      return bVal - aVal
    })
    sortIndex++
  }
  return listArr.map(x => x.join('.'))
}

测试代码

ts
import { expect, test } from 'vitest'
import { versionListSort } from './typescript.ts'
import { versionListSort as versionListSortJs } from './javascript.js'

const versionList = ['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']
const versionListRes = ['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1']

test(`['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5'] toEqual ['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1']`, () => {
  expect(versionListSort(versionList)).toEqual(versionListRes)
})

test(`['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5'] toEqual ['4.3.5', '4.3.4.5', '4.2', '2.3.3', '0.302.1', '0.1.1']`, () => {
  expect(versionListSortJs(versionList)).toEqual(versionListRes)
})

const versionList1 = ['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5', '4.2.3']
const versionList1Res = ['4.3.5', '4.3.4.5', '4.2.3', '4.2', '2.3.3', '0.302.1', '0.1.1']

test(`['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5', '4.2.3'] toEqual ['4.3.5', '4.3.4.5', '4.2.3', '4.2', '2.3.3', '0.302.1', '0.1.1']`, () => {
  expect(versionListSort(versionList1)).toEqual(versionList1Res)
})

test(`['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5', '4.2.3'] toEqual ['4.3.5', '4.3.4.5', '4.2.3', '4.2', '2.3.3', '0.302.1', '0.1.1']`, () => {
  expect(versionListSortJs(versionList1)).toEqual(versionList1Res)
})