code: private void populateSortedCustomFieldsList ()
     {
        m_sortedCustomFieldsList = new ArrayList<CustomField>();
        for (CustomField field : m_projectFile.getCustomFields())
        {
           FieldType fieldType = field.getFieldType();
           if (fieldType != null)
           {
              m_sortedCustomFieldsList.add(field);
           }
        }
  
        // Sort to ensure consistent order in file
        Collections.sort(m_sortedCustomFieldsList, new Comparator<CustomField>()
        {
           @Override public int compare(CustomField customField1, CustomField customField2)
           {
              FieldType o1 = customField1.getFieldType();
              FieldType o2 = customField2.getFieldType();
              String name1 = o1.getClass().getSimpleName() + "." + o1.getName() + " " + customField1.getAlias();
              String name2 = o2.getClass().getSimpleName() + "." + o2.getName() + " " + customField2.getAlias();
              return name1.compareTo(name2);
           }
        });
     }
docs: Populate a sorted list of custom fields to ensure that these fields
  are written to the file in a consistent order.