prasule/lib/util/sorting.dart
Matyáš Caras 943bf15aab
feat: add basic sorting (#9)
also made some cosmetic changes elsewhere,
2024-01-29 20:19:03 +01:00

59 lines
1.8 KiB
Dart

import 'package:grouped_list/grouped_list.dart';
import 'package:intl/intl.dart';
/// Sorts [GroupedListView]'s group by newest group
int groupSortNewest(String a, String b, String locale) {
// TODO: better sorting algorithm lol
final yearA = RegExp(r'\d+').firstMatch(a);
if (yearA == null) return 0;
final yearB = RegExp(r'\d+').firstMatch(b);
if (yearB == null) return 0;
final compareYears = int.parse(yearB.group(0)!).compareTo(
int.parse(yearA.group(0)!),
);
if (compareYears != 0) {
return compareYears;
}
final months = List<String>.generate(
12,
(index) => DateFormat.MMMM(locale).format(
DateTime(2023, index + 1),
),
);
final monthA = RegExp('[^0-9 ]+').firstMatch(a);
if (monthA == null) return 0;
final monthB = RegExp('[^0-9 ]+').firstMatch(b);
if (monthB == null) return 0;
return months.indexOf(monthB.group(0)!).compareTo(
months.indexOf(monthA.group(0)!),
);
}
/// Sorts [GroupedListView]'s group by oldest group
int groupSortOldest(String a, String b, String locale) {
// TODO: better sorting algorithm lol
final yearA = RegExp(r'\d+').firstMatch(a);
if (yearA == null) return 0;
final yearB = RegExp(r'\d+').firstMatch(b);
if (yearB == null) return 0;
final compareYears = int.parse(yearA.group(0)!).compareTo(
int.parse(yearB.group(0)!),
);
if (compareYears != 0) {
return compareYears;
}
final months = List<String>.generate(
12,
(index) => DateFormat.MMMM(locale).format(
DateTime(2023, index + 1),
),
);
final monthA = RegExp('[^0-9 ]+').firstMatch(a);
if (monthA == null) return 0;
final monthB = RegExp('[^0-9 ]+').firstMatch(b);
if (monthB == null) return 0;
return months.indexOf(monthA.group(0)!).compareTo(
months.indexOf(monthB.group(0)!),
);
}