1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 15:09:14 +00:00

[my libraries] sort by name & time (#2726)

This commit is contained in:
llj
2018-12-28 15:19:10 +08:00
committed by Daniel Pan
parent dd15cd0ea8
commit aa98e831e6
6 changed files with 179 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import { mediaUrl, gettext } from './constants';
import { strChineseFirstPY } from './pinyin-by-unicode';
export const Utils = {
@@ -368,5 +369,115 @@ export const Utils = {
return false;
}
}
},
compareTwoWord: function(wordA, wordB) {
// compare wordA and wordB at lower case
// if wordA >= wordB, return 1
// if wordA < wordB, return -1
var a_val, b_val,
a_uni = wordA.charCodeAt(0),
b_uni = wordB.charCodeAt(0);
if ((19968 < a_uni && a_uni < 40869) && (19968 < b_uni && b_uni < 40869)) {
// both are chinese words
a_val = strChineseFirstPY.charAt(a_uni - 19968).toLowerCase();
b_val = strChineseFirstPY.charAt(b_uni - 19968).toLowerCase();
} else if ((19968 < a_uni && a_uni < 40869) && !(19968 < b_uni && b_uni < 40869)) {
// a is chinese and b is english
return 1;
} else if (!(19968 < a_uni && a_uni < 40869) && (19968 < b_uni && b_uni < 40869)) {
// a is english and b is chinese
return -1;
} else {
// both are english words
a_val = wordA.toLowerCase();
b_val = wordB.toLowerCase();
return this.compareStrWithNumbersIn(a_val, b_val);
}
return a_val >= b_val ? 1 : -1;
},
// compare two strings which may have digits in them
// and compare those digits as number instead of string
compareStrWithNumbersIn: function(a, b) {
var reParts = /\d+|\D+/g;
var reDigit = /\d/;
var aParts = a.match(reParts);
var bParts = b.match(reParts);
var isDigitPart;
var len = Math.min(aParts.length, bParts.length);
var aPart, bPart;
if (aParts && bParts &&
(isDigitPart = reDigit.test(aParts[0])) == reDigit.test(bParts[0])) {
// Loop through each substring part to compare the overall strings.
for (var i = 0; i < len; i++) {
aPart = aParts[i];
bPart = bParts[i];
if (isDigitPart) {
aPart = parseInt(aPart, 10);
bPart = parseInt(bPart, 10);
}
if (aPart != bPart) {
return aPart < bPart ? -1 : 1;
}
// Toggle the value of isDigitPart since the parts will alternate.
isDigitPart = !isDigitPart;
}
}
// Use normal comparison.
return (a >= b) - (a <= b);
},
sortRepos: function(repos, sortBy, sortOrder) {
const _this = this;
let comparator;
switch (`${sortBy}-${sortOrder}`) {
case 'name-asc':
comparator = function(a, b) {
if (!a.repo_name) {
return 1;
}
if (!b.repo_name) {
return -1;
}
var result = _this.compareTwoWord(a.repo_name, b.repo_name);
return result;
};
break;
case 'name-desc':
comparator = function(a, b) {
if (!a.repo_name) {
return -1;
}
if (!b.repo_name) {
return 1;
}
var result = _this.compareTwoWord(a.repo_name, b.repo_name);
return -result;
};
break;
case 'time-asc':
comparator = function(a, b) {
return a.last_modified < b.last_modified ? -1 : 1;
};
break;
case 'time-desc':
comparator = function(a, b) {
return a.last_modified < b.last_modified ? 1 : -1;
};
break;
}
repos.sort(comparator);
return repos;
}
};