如何用Java对集合进行排序?

时间:2012-10-24 06:53:14

标签: java geotools

我尝试创建一个项目,使用GDAL / OGR将.mif / .tab文件中的数据写入Data Base。我这样做:

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(dbFeatureType);
SimpleFeatureCollection collection = FeatureCollections.newCollection();
while (mifReader.hasNext()){      
  in = (SimpleFeatureImpl) mifReader.next();
  SimpleFeature dbFeature = featureBuilder.buildFeature(null);
  for(AttributeDescriptor ad : adList){
    String name = ad.getLocalName();
    Object attrValue;
    attrValue = in.getAttribute(name);
    if(attrValue instanceof String){
      String string3 = new String((attrValue.toString()).getBytes("ISO-8859-1"), "cp1251");
      if(name.trim().equalsIgnoreCase(kadname.trim())){
        dbFeature.setAttribute("kadnum", string3);
      }
    }
    if (attrValue instanceof Geometry){
        idn++;
        com.vividsolutions.jts.geom.Geometry geom = (Geometry) in.getAttribute(name);
        dbFeature.setAttribute("id", idn);
        System.out.println("after insrt="+idn);
        dbFeature.setAttribute("deleted", deleted);
        dbFeature.setAttribute("the_geom", geom);
        dbFeature.setAttribute("status_id", status_id);

    }
    collection.add(dbFeature);
    }
}

来自.mid文件的简单数据:

__id_|___kadnum_____

  3,"66:2:00:88 951"
  4,"66:2:00:88 952"
  5,"66:2:00:88 954"

在db中我得到了这个:

enter image description here

我的数据是2-4行。你看到我得到增量变量idn并将其放在属性id中。从.mid获取kadnum并将其放入属性kadnum 它很好,但在bd我得到错误的顺序行。我的意思是属性kadnum=66:2:00:88 951的元素将在db的第二行,但在第4行  它的意思是我要将集合中的项目顺序改为相反。它正确的解决方案?怎么做?或者GDAL可以自己做吗?

更新

我有:

 SimplePropertyImpl in =(SimpleFeatureImpl) mifReader.next();

我试试:

in.getProperty("id").getName() ;

我没有得到一个PropertyName我得到一个字符串。我做错了什么?

1 个答案:

答案 0 :(得分:1)

是否要根据id

对数据库行进行排序
select * from <yourtable> order by id asc;

如果您想在代码中对FeatureCollection进行排序,可能需要查看SimpleFeatureCollection.sortSortBy。我期待像

这样的东西
collection.sort( new SortBy() 
                 { 
                     @Override PropertyName getPropertyName() { return YourPropertyNameImpl( "id" ); } 
                     @Override SortOrder getSortOrder() { return SortOrder.ASCENDING; }
                 }

会让你开始朝着正确的方向前进。

干杯,