Merge pull request #5456 from zhoumeina/pr/fix_access_level_sort

Fix access level can not sort
This commit is contained in:
Qian Deng 2018-08-02 22:56:56 -04:00 committed by GitHub
commit 4cb0a3ee52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -58,7 +58,7 @@ export class ListProjectComponent implements OnDestroy {
roleInfo = RoleInfo;
repoCountComparator: Comparator<Project> = new CustomComparator<Project>("repo_count", "number");
timeComparator: Comparator<Project> = new CustomComparator<Project>("creation_time", "date");
accessLevelComparator: Comparator<Project> = new CustomComparator<Project>("public", "number");
accessLevelComparator: Comparator<Project> = new CustomComparator<Project>("public", "string");
roleComparator: Comparator<Project> = new CustomComparator<Project>("current_user_role_id", "number");
currentPage = 1;
totalCount = 0;

View File

@ -143,8 +143,23 @@ export class CustomComparator<T> implements Comparator<T> {
compare(a: { [key: string]: any | any[] }, b: { [key: string]: any | any[] }) {
let comp = 0;
if (a && b) {
let fieldA = a[this.fieldName];
let fieldB = b[this.fieldName];
let fieldA, fieldB;
for (let key of Object.keys(a)) {
if (key === this.fieldName) {
fieldA = a[key];
fieldB = b[key];
break;
} else if (typeof a[key] === 'object') {
let insideObject = a[key];
for (let insideKey in insideObject) {
if (insideKey === this.fieldName) {
fieldA = insideObject[insideKey];
fieldB = b[key][insideKey];
break;
}
}
}
}
switch (this.type) {
case "number":
comp = fieldB - fieldA;
@ -152,6 +167,9 @@ export class CustomComparator<T> implements Comparator<T> {
case "date":
comp = new Date(fieldB).getTime() - new Date(fieldA).getTime();
break;
case "string":
comp = fieldB.localeCompare(fieldA);
break;
}
}
return comp;