sortedByPublicFirstThenSyntheticLast method

List<T> sortedByPublicFirstThenSyntheticLast()

Sorts declarations with public visibility first and synthetic declarations last.

Returns:

  • New List with ordering: public > private > synthetic
  • Original order preserved among declarations with same characteristics

Example:

final fields = classDecl.getFields()
  .sortedByPublicFirstThenSyntheticLast();
// [publicField1, publicField2, _privateField1, @syntheticField]

Sorting Logic:

1. Synthetic declarations always last
2. Among non-synthetic:
   - Public   -> -1 (first)
   - Private  ->  1 (after public)
   - Equal    ->  0 (original order)

Implementation

List<T> sortedByPublicFirstThenSyntheticLast() {
  return toList()
    ..sort((a, b) {
      // Step 1: Synthetic comes last
      final syntheticA = a.getIsSynthetic();
      final syntheticB = b.getIsSynthetic();
      if (syntheticA != syntheticB) {
        return syntheticA ? 1 : -1;
      }

      // Step 2: Public comes before private
      final publicA = a.getIsPublic();
      final publicB = b.getIsPublic();
      if (publicA == publicB) return 0;
      return publicA ? -1 : 1;
    });
}