sortObjectKeysAsc function

dynamic sortObjectKeysAsc(
  1. dynamic term
)

Implementation

dynamic sortObjectKeysAsc(dynamic term) {
  if (term is List) {
    return term.map((item) => sortObjectKeysAsc(item)).toList();
  } else if (term is Map) {
    // In Dart, maps have keys that are always strings, so we can sort them
    final sortedMap = Map.fromEntries(
      term.entries.toList()..sort((a, b) => a.key.compareTo(b.key)),
    );
    return sortedMap
        .map((key, value) => MapEntry(key, sortObjectKeysAsc(value)));
  } else if (term is MapEntry) {
    // MapEntry is Dart specific, so we handle it separately
    return MapEntry(term.key, sortObjectKeysAsc(term.value));
  } else if (term is Map<String, dynamic>) {
    final sortedKeys = term.keys.toList()..sort();
    return Map.fromEntries(
      sortedKeys.map((key) => MapEntry(key, sortObjectKeysAsc(term[key]))),
    );
  } else {
    return term;
  }
}