Skip to content

Commit

Permalink
fix(sorting): sorts incoming links by res class then res label (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijeinath committed Sep 1, 2023
1 parent 7fc4826 commit 85f1af6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 54 deletions.
40 changes: 0 additions & 40 deletions apps/dsp-app/src/app/main/services/sorting.service.spec.ts
Expand Up @@ -144,36 +144,6 @@ describe('SortingService', () => {
},
]);
});

it('should return an array sorted by lastname reversed', () => {
const sorted = service.keySortByAlphabetical(
data,
'lastname',
true
);
expect(sorted).toEqual([
{
firstname: 'Mickey',
lastname: 'Mouse',
creator: 'Walt Disney',
},
{
firstname: 'Gaston',
lastname: 'Lagaffe',
creator: 'André Franquin',
},
{
firstname: 'Gyro',
lastname: 'Gearloose',
creator: 'Carl Barks',
},
{
firstname: 'Charlie',
lastname: 'Brown',
creator: 'Charles M. Schulz',
},
]);
});
});

describe('Sort an array of ReadProject', () => {
Expand Down Expand Up @@ -208,15 +178,5 @@ describe('SortingService', () => {

expect(sorted).toEqual([project1, project2, project3, project4]);
});

it('should sort an array of ReadProject by "longname" reversed', () => {
const sorted = service.keySortByAlphabetical(
projects,
'longname',
true
);

expect(sorted).toEqual([project4, project3, project2, project1]);
});
});
});
37 changes: 25 additions & 12 deletions apps/dsp-app/src/app/main/services/sorting.service.ts
Expand Up @@ -14,33 +14,46 @@ export class SortingService {
}

/**
* compares value by value and sorts in alphabetical order using the provided key
* optionally, you can have the array returned to you in reversed order by setting the reversed parameter to 'true'
* compares value by value and sorts in alphabetical order using the provided first key, in case the comparison
* of the values results in zero the second key is used if provided.
*/
keySortByAlphabetical<T extends object>(
value: Array<T>,
sortKey: keyof T,
reversed = false
firstSortKey: keyof T,
secondSortKey?: keyof T
): Array<T> {
const sortedArray = value.slice();
sortedArray.sort((a: T, b: T) => {
if (
String(a[sortKey]).toLowerCase() <
String(b[sortKey]).toLowerCase()
String(a[firstSortKey]).toLowerCase() <
String(b[firstSortKey]).toLowerCase()
) {
return -1;
} else if (
String(a[sortKey]).toLowerCase() >
String(b[sortKey]).toLowerCase()
String(a[firstSortKey]).toLowerCase() >
String(b[firstSortKey]).toLowerCase()
) {
return 1;
} else {
return 0;
if (secondSortKey) {
if (
String(a[secondSortKey]).toLowerCase() <
String(b[secondSortKey]).toLowerCase()
) {
return -1;
} else if (
String(a[secondSortKey]).toLowerCase() >
String(b[secondSortKey]).toLowerCase()
) {
return 1;
} else {
return 0;
}
} else {
return 0
}
}
});
if (reversed) {
sortedArray.reverse();
}
return sortedArray;
}
}
Expand Up @@ -63,6 +63,7 @@ import {
ValueOperationEventService,
} from '../services/value-operation-event.service';
import { ValueService } from '../services/value.service';
import { SortingService } from '@dsp-app/src/app/main/services/sorting.service';

// object of property information from ontology class, properties and property values
export interface PropertyInfoValues {
Expand Down Expand Up @@ -177,7 +178,8 @@ export class PropertiesComponent implements OnInit, OnChanges, OnDestroy {
private _valueOperationEventService: ValueOperationEventService,
private _valueService: ValueService,
private _componentCommsService: ComponentCommunicationEventService,
private _projectService: ProjectService
private _projectService: ProjectService,
private _sortingService: SortingService,
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -672,7 +674,11 @@ export class PropertiesComponent implements OnInit, OnChanges, OnDestroy {
.subscribe(
(response: ReadResourceSequence) => {
if (response.resources.length > 0) {
this.incomingLinkResources = response.resources;
this.incomingLinkResources = this._sortingService.keySortByAlphabetical(
response.resources,
"resourceClassLabel",
"label"
);
}
this.loading = false;
},
Expand Down

0 comments on commit 85f1af6

Please sign in to comment.