42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
function containsLetter(str) {
|
|
return /[a-zA-Z]/.test(str)
|
|
}
|
|
|
|
function splitAndConvertToIntIfPossible(str, delimiter = '-') {
|
|
return str.split(delimiter).map((part) => {
|
|
const num = parseInt(part, 10)
|
|
return isNaN(num) ? part : num
|
|
})
|
|
}
|
|
|
|
export function woSort(v1, v2, field) {
|
|
// Check if either name is null or empty string and prioritize non-null/non-empty names
|
|
if (v1[field] == null || v1[field] === '') {
|
|
return v2[field] == null || v2[field] === '' ? 0 : 1
|
|
}
|
|
if (v2[field] == null || v2[field] === '') {
|
|
return -1
|
|
}
|
|
|
|
// First check if either string contains a letter
|
|
const hasLetter1 = containsLetter(v1[field])
|
|
const hasLetter2 = containsLetter(v2[field])
|
|
|
|
// If one contains a letter and the other does not, prioritize the one with a letter
|
|
if (hasLetter1 !== hasLetter2) {
|
|
return hasLetter1 ? -1 : 1
|
|
}
|
|
|
|
// Otherwise, split and convert to appropriate types for comparison as before
|
|
const parts1 = splitAndConvertToIntIfPossible(v1[field])
|
|
const parts2 = splitAndConvertToIntIfPossible(v2[field])
|
|
|
|
for (let i = 0; i < Math.min(parts1.length, parts2.length); i++) {
|
|
if (parts1[i] < parts2[i]) return -1
|
|
if (parts1[i] > parts2[i]) return 1
|
|
}
|
|
|
|
// If all compared parts are equal, the shorter one is considered smaller
|
|
return parts1.length - parts2.length
|
|
}
|