I have a list, it’s already sorted in alphabetical order. I’d like to break it into sections corresponding with the letter is starts with, like so:
A
- aListItem1
- aListItem2
- aListItem3
B
- bListItem1
- bListItem2
- bListItem3
C
- cListItem1
- cListItem2
- cListItem3
…
I could do something like:
foreach item in list
if item startsWith A
… do something
if item startsWith B
… do something
if item startsWith C
… do something
etc…
but is there a more concise way to do this?
Just keep track of the current first letter as you go: