cofounderella/lib/utils/list_utils.dart

39 lines
1.3 KiB
Dart
Raw Permalink Normal View History

2024-06-14 14:28:59 +02:00
import 'package:collection/collection.dart';
2024-06-21 02:42:45 +02:00
import '../enumerations.dart';
2024-06-14 14:28:59 +02:00
import '../models/language.dart';
/// Compare two lists by their content ignoring their elements order.
bool equalContent(List<dynamic> list1, List<dynamic> list2) {
return const DeepCollectionEquality.unordered().equals(list1, list2);
}
2024-06-21 02:42:45 +02:00
/// Sort the given list of languages so that German appears first,
/// English second, and all other languages follow in alphabetical order.
2024-06-14 14:28:59 +02:00
List<Language> sortLanguageList(List<Language> langList) {
List<Language> sortedLanguages = List.from(langList);
sortedLanguages.sort((a, b) {
// German first
if (a.code == 'de') return -1;
if (b.code == 'de') return 1;
// English second
if (a.code == 'en') return -1;
if (b.code == 'en') return 1;
// All others by name ascending
return a.name.compareTo(b.name);
});
return sortedLanguages;
}
2024-06-21 02:42:45 +02:00
/// Sort the given list of sectors of interest, placing [SectorOption.other] at the end.
List<SectorOption> sortSectorsList(List<SectorOption> sectorList) {
List<SectorOption> sortedSectors = List.from(sectorList);
sortedSectors.sort((a, b) {
// other last
if (a.name == 'other') return 1;
if (b.name == 'other') return -1;
// All others by name ascending
return a.name.compareTo(b.name);
});
return sortedSectors;
}