Last active
October 13, 2023 12:25
-
-
Save abdorll/a78d90280af6e2bac7faa1d357bf3daf to your computer and use it in GitHub Desktop.
sort list of names with title
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
getNameTitlesWithNames(childNames).forEach((e){ | |
print("\n${e.letterTitle!}"); | |
print(e.nameList); | |
}); | |
} | |
class EachTitledNameModel{ | |
String? letterTitle; | |
List<String>? nameList; | |
EachTitledNameModel ({this.letterTitle, this.nameList}); | |
} | |
List<EachTitledNameModel> getNameTitlesWithNames(List<String> nameList){ | |
List<EachTitledNameModel> sortedNameWithTitle = []; | |
nameList.sort((a, b)=> a.compareTo(b)); | |
for(int i = 0; i<nameList.length; i++){ | |
String thisTitle= nameList[i][0]; | |
if(sortedNameWithTitle.any((e)=> e.letterTitle== thisTitle)){ | |
continue; | |
} | |
else{ | |
sortedNameWithTitle.add(EachTitledNameModel(letterTitle: thisTitle,nameList: nameList.where((e)=> e[0]==thisTitle).toList())); | |
} | |
} | |
return sortedNameWithTitle; | |
} | |
List<String> childNames = [ | |
"Olivia", | |
"Liam", | |
"Emma", | |
"Noah", | |
"Ava", | |
"Isabella", | |
"Sophia", | |
"Mia", | |
"Charlotte", | |
"Amelia", | |
"Harper", | |
"Evelyn", | |
"Abigail", | |
"Emily", | |
"Elizabeth", | |
"Sofia", | |
"Scarlett", | |
"Grace", | |
"Lily", | |
"Avery", | |
"Ella", | |
"Chloe", | |
"Victoria", | |
"Aria", | |
"Madison", | |
"Layla", | |
"Riley", | |
"Zoe", | |
"Nora", | |
"Lily", | |
"Ethan", | |
"William", | |
"James", | |
"Benjamin", | |
"Henry", | |
"Alexander", | |
"Michael", | |
"Daniel", | |
"Matthew", | |
"Jack", | |
"Samuel", | |
"Joseph", | |
"David", | |
"Andrew", | |
"Noah", | |
"Oliver", | |
"Lucas", | |
"Mason", | |
"Logan", | |
"Caleb", | |
"Elijah", | |
"William", | |
"Gabriel", | |
"Jackson", | |
"Owen", | |
"Joseph", | |
"Aiden", | |
"Sebastian", | |
"Julian", | |
"Wyatt" | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment