Question about sorting some data


#1

I'm trying to sort some data and not having much luck.

Example:
I'm writing to a map like this...
state.stuffMap += "${evt.displayName} — stuff about the device<br>"

Later I'm showing this info on a page within an app, like this...
paragraph "${state.stuffMap}"

Which is giving a display of...

Device1 - stuff about device 1
Device3 - stuff about device 3
Device8 - stuff about device 8
Device2 - stuff about device 2

etc, etc, etc.

Everything about the app is working awesome and is ready for release but I would love to sort that file in alphabetical order. Is this possible?

Thanks


#2

Is stuff map actually a map?, or is it a list...
If it's a simple list .sort() will do it


#3

I tried
state.stuffMap.sort()
paragraph "${state.stuffMap}"

and

stuffMap.sort()
paragraph "${state.stuffMap}"

but got an error on both

Everything I can find wants to sort on Key but as you can see there is no key, value type of format here. So I was thinking list too but it just errors out.

Basically as things happen that fit the criteria it writes to the list/map using the statement in the first post. I tried doing something with a key, value but just couldn't get it to work they way I wanted it to. Then I came across this way in Grail's Cookbook. A great reference for groovy examples.


#4

This is how I tried to sort...

if(state.motionMap) {
	LOGDEBUG("In pageCounts...Motion Sensors")
    state.motionMap.sort()
	paragraph "${state.motionMap}"
}

and got this instead...

Unexpected Error

An unexpected error has occurred trying to load the app. Check Logs for more information.

Error: No signature of method: java.lang.String.sort() is applicable for argument types: () values: [] Possible solutions: drop(int), tr(java.lang.CharSequence, java.lang.CharSequence), wait(), split(), toSet(), next()


#5

Yeah, it's a map, sort doesn't work on maps, they are imutable...
Create a list to extract the needed elements from the map, then sort that, or make the data structure a list to start with.