title: Grails getProperties method does not always return properties in the correct order
body: <p>I have a class that looks like this:</p>
  
  <pre><code>class Foo {
      name
      description
  
      static constraints = {
          name()
          description()
      }
  }
  </code></pre>
  
  <p>I want to add display instances of my class in a <a href="http://flexigrid.info/" rel="nofollow">Flexigrid</a>.  When data is sent to a flexigrid it needs to be in a format like JSON or XML... I have chosen JSON.  Flexigrid expects JSON arrays it receives to have the following format:</p>
  
  <pre><code>{
      "page": "1",
      "total": "1",
      "rows": [
          {
              "id": "1",
              "cell": [
                  "1",
                  "The name of Foo 1",
                  "The description of Foo 1"
              ]
          },
          {
              "id": "2",
              "cell": [
                  "2",
                  "The name of Foo 2",
                  "The description of Foo 2"
              ]
          }
      ]
  }
  </code></pre>
  
  <p>To get my <code>Foo</code> objects into this format I do something similar to this:</p>
  
  <pre><code>def foos = Foo.getAll( 1, 2 )
  
  def results = [:]
  results[ "page" ] = params.page
  results[ "total" ] = foos.size()
  results[ "rows" ] = []
  
  for( foo in foos ) {
      def cell = []
      cell.add( foo.id )
  
      foo.getProperties().each() { key, value -&gt; // Sometimes get foo.getProperties().each() returns foo.description then foo.name instead of foo.name then foo.description as desired.
          cell.add( value.toString() )
      }
  
      results[ "rows" ].add( [ "id": foo.id, "cell": cell ] )
  }
  
  render results as JSON
  </code></pre>
  
  <p>The problem is that every once in a while <code>foo.getProperties().each()</code> returns <code>foo.description</code> then <code>foo.name</code> resulting in <code>foo.description</code> being put in the name column of my flexigrid and <code>foo.name</code> being put in the description column of my flexigrid for a specific row.</p>
  
  <p>I tried specifying constraints in the <code>Foo</code> domain class so the <code>getProperties</code> would return in the correct order, but it didn't work.  <strong>How can I make sure <code>getProperties</code> returns properties in a predictable order?</strong></p>
  
  <p><strong>This is how I fixed this issuse:</strong></p>
  
  <pre><code>def items = Foo.getAll()
  
  for( item in items ) {
      def cell = []
      cell.add( item.id )
      Foo.constraints.each() { key, value -&gt;
          def itemValue = item.getProperty( key )
          if( !( itemValue instanceof Collection ) ) {
              cell.add( itemValue.toString() )
          }
      }
  }
  </code></pre>
  
  <p>So <code>Foo.constraints</code> gets a map of constraints where each constraint is an instance of <code>Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry</code>.  After testing I have found this map always returns my <code>Foo</code> static constraints in the order I entered them (also confirmed by Ian).  Now only the properties of the <code>item</code> which are in <code>Foo.constraints</code> will be added to the <code>cell</code> for flexigrid.</p>