Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  [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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading