dc10
dc10

Reputation: 2208

Play framework how to iterate with ordered index

I am using play framework 2.3.8 and I need to iterate a java HashMap which I do like this in the template:

 @(city: String, intents: java.util.Map[Intent, TimeTable.Row], lang: Lang)

@for((item, index) <- intents.entrySet.zipWithIndex) {
            <li>Item @index </li>
        }

The problem now is that I got indexes as follows:

Item 7 Item 17 Item 22 Item 8 Item 28 Item 23 Item 33 Item 18 Item 5 Item 25 Item 11 Item 16

How is it possible to get ordered indices and why is the list here unordered?

Upvotes: 0

Views: 109

Answers (2)

Michael Nedokushev
Michael Nedokushev

Reputation: 50

I recommend you to use SortedMap, and it's better not to transform your HashMap into something else inside the template, but use SortedMap from the start.

Upvotes: 0

Odomontois
Odomontois

Reputation: 16328

Just clarifing Carlos's comment

You could use implicit scala to java convertion:

@import scala.collection.JavaConversions._

@(city: String, intents: java.util.Map[Intent, TimeTable.Row], lang: Lang)

@for(((intent,row), index) <- intents.toList.zipWithIndex) {
    <li>Item @index : <strong>@intent</strong> <i>row</i></li>
}

Upvotes: 1

Related Questions