How to sort alphabetically by start at small letter and big letter

I have a question for sorting in list which is giving me some drastic time to think, Don’t have idea how am suppose to do by my question how can i achieve this result

e.g.

final list = ["an","And","another","v","V","bb","a","B"];  
print(list..sort((a,b)=> a.toLowerCase().compareTo(b.toLowerCase())));
 // as result is [a, an, And, another, B, bb, v, V]   

But i want is like this

  [a,an,another,And,bb,B,v,V]

>Solution :

You need to get a bit more involved in your comparison. This is close, and I’m sure you can take it all the way:

void main() {
  final list = ["an","And","another","v","V","bb","a","B"];
  print(list..sort((a,b) {
    if (a[0].toLowerCase() == b[0].toLowerCase()) {
      return a.compareTo(b);
    }
    else {
      return a.toLowerCase().compareTo(b.toLowerCase());
    }
  }));
}
// [And, a, an, another, B, bb, V, v]

Leave a Reply