ArcEngine的排序方法有多种,下面介绍一下主要的四种方法。

准备数据

测试数据如下图所示:新建一个Geodatabase的要素类,其中Name为道路名称,Width为道路宽度,下面将根据Width字段进行倒序排序。

方法一:IQueryFilterDefinition接口

利用IQueryFilterDefinition接口我们可以定义排序语句,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;namespace WindowsFormsApplication1
{public partial class MainForm : Form{public MainForm(){InitializeComponent();axMapControl1.LoadMxFile(@"E:\Users\dsf\Desktop\无标题.mxd");}private void btnSort_Click(object sender, EventArgs e){IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;DataTable dataTable = GetDataTable(pFeatureLayer.FeatureClass);dataGridView1.DataSource = dataTable;}private DataTable GetDataTable(IFeatureClass pFeatureClass){DataTable dataTable = new DataTable();dataTable.Columns.Add("OBJECTID");dataTable.Columns.Add("Name");dataTable.Columns.Add("Width");dataTable.Columns.Add("SHAPE_Length");// 字段索引int fieldIndex_OBJECTID = pFeatureClass.Fields.FindField("OBJECTID");int fieldIndex_Name = pFeatureClass.Fields.FindField("Name");int fieldIndex_Width = pFeatureClass.Fields.FindField("Width");int fieldIndex_SHAPE_Length = pFeatureClass.Fields.FindField("SHAPE_Length");// 字段排序IQueryFilter pQueryFilter = new QueryFilter();IQueryFilterDefinition pQueryFilterDefinition = pQueryFilter as IQueryFilterDefinition;pQueryFilterDefinition.PostfixClause = "order by Width desc";// 要素游标IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, true);IFeature pFeature = pFeatureCursor.NextFeature();while (pFeature != null){DataRow dataRow = dataTable.NewRow();dataRow[0] = pFeature.get_Value(fieldIndex_OBJECTID).ToString();dataRow[1] = pFeature.get_Value(fieldIndex_Name).ToString();dataRow[2] = pFeature.get_Value(fieldIndex_Width).ToString();dataRow[3] = pFeature.get_Value(fieldIndex_SHAPE_Length).ToString();dataTable.Rows.Add(dataRow);pFeature = pFeatureCursor.NextFeature();}// 释放游标System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);return dataTable;}}
}

排序结果如下图所示:

方法二:IQueryDef2接口

利用IQueryDef2也可以实现排序功能,该接口只对Geodatabase的数据有效,不支持shp文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;namespace WindowsFormsApplication1
{public partial class MainForm : Form{public MainForm(){InitializeComponent();axMapControl1.LoadMxFile(@"E:\Users\dsf\Desktop\无标题.mxd");}private void btnSort_Click(object sender, EventArgs e){IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;DataTable dataTable = GetDataTable(pFeatureLayer.FeatureClass);dataGridView1.DataSource = dataTable;}private DataTable GetDataTable(IFeatureClass pFeatureClass){DataTable dataTable = new DataTable();dataTable.Columns.Add("OBJECTID");dataTable.Columns.Add("Name");dataTable.Columns.Add("Width");dataTable.Columns.Add("SHAPE_Length");// 字段索引ITable pTable = pFeatureClass as ITable;int fieldIndex_OBJECTID = pTable.Fields.FindField("OBJECTID");int fieldIndex_Name = pTable.Fields.FindField("Name");int fieldIndex_Width = pTable.Fields.FindField("Width");int fieldIndex_SHAPE_Length = pTable.Fields.FindField("SHAPE_Length");// 获取工作空间IDataset pDataset = pFeatureClass as IDataset;IWorkspace pWorkspace = pDataset.Workspace;IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;// 字段排序IQueryDef2 pQueryDef = pFeatureWorkspace.CreateQueryDef() as IQueryDef2;pQueryDef.Tables = pFeatureClass.AliasName;pQueryDef.PostfixClause = "order by Width desc";// 要素游标ICursor pCursor = pQueryDef.Evaluate2(true);IRow pRow = pCursor.NextRow();while (pRow != null){DataRow dataRow = dataTable.NewRow();dataRow[0] = pRow.get_Value(fieldIndex_OBJECTID).ToString();dataRow[1] = pRow.get_Value(fieldIndex_Name).ToString();dataRow[2] = pRow.get_Value(fieldIndex_Width).ToString();dataRow[3] = pRow.get_Value(fieldIndex_SHAPE_Length).ToString();dataTable.Rows.Add(dataRow);pRow = pCursor.NextRow();}// 释放游标System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);return dataTable;}}
}

排序结果如下图所示:

方法三:ITableSort接口

利用ITableSort接口也可以实现排序效果。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;namespace WindowsFormsApplication1
{public partial class MainForm : Form{public MainForm(){InitializeComponent();axMapControl1.LoadMxFile(@"E:\Users\dsf\Desktop\无标题.mxd");}private void btnSort_Click(object sender, EventArgs e){IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;DataTable dataTable = GetDataTable(pFeatureLayer.FeatureClass);dataGridView1.DataSource = dataTable;}private DataTable GetDataTable(IFeatureClass pFeatureClass){DataTable dataTable = new DataTable();dataTable.Columns.Add("OBJECTID");dataTable.Columns.Add("Name");dataTable.Columns.Add("Width");dataTable.Columns.Add("SHAPE_Length");// 字段索引ITable pTable = pFeatureClass as ITable;int fieldIndex_OBJECTID = pTable.Fields.FindField("OBJECTID");int fieldIndex_Name = pTable.Fields.FindField("Name");int fieldIndex_Width = pTable.Fields.FindField("Width");int fieldIndex_SHAPE_Length = pTable.Fields.FindField("SHAPE_Length");// 字段排序ITableSort pTableSort = new TableSort();pTableSort.Table = pFeatureClass as ITable;pTableSort.Fields = "Width";pTableSort.set_Ascending("Width", false);pTableSort.Sort(null);// 要素游标ICursor pCursor = pTableSort.Rows;IRow pRow = pCursor.NextRow();while (pRow != null){DataRow dataRow = dataTable.NewRow();dataRow[0] = pRow.get_Value(fieldIndex_OBJECTID).ToString();dataRow[1] = pRow.get_Value(fieldIndex_Name).ToString();dataRow[2] = pRow.get_Value(fieldIndex_Width).ToString();dataRow[3] = pRow.get_Value(fieldIndex_SHAPE_Length).ToString();dataTable.Rows.Add(dataRow);pRow = pCursor.NextRow();}// 释放游标System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);return dataTable;}}
}

排序结果如下图所示:

方法四:ITableSort + ITableSortCallBack实现自定义排序

很多情况下我们需要定义我们自己的排序规则,现在将数据表修改一下,如下图所示:Info字段记录了每条道路的名称和宽度,中间以"_"进行分隔。

现在我们希望只根据每条道路的宽度进行倒序排序,如果还是采用上述方法,你可能会得到这样的结果:

很明显这不是我们想要的结果,不过还好ArcEngine给我们提供了一个ITableSortCallBack接口,利用这个接口我们可以自己定义排序规则,代码如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;namespace WindowsFormsApplication1
{public partial class MainForm : Form{public MainForm(){InitializeComponent();axMapControl1.LoadMxFile(@"E:\Users\dsf\Desktop\无标题.mxd");}private void btnSort_Click(object sender, EventArgs e){IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;DataTable dataTable = GetDataTable(pFeatureLayer.FeatureClass);dataGridView1.DataSource = dataTable;}private DataTable GetDataTable(IFeatureClass pFeatureClass){DataTable dataTable = new DataTable();dataTable.Columns.Add("OBJECTID");dataTable.Columns.Add("SHAPE_Length");dataTable.Columns.Add("Info");// 字段索引ITable pTable = pFeatureClass as ITable;int fieldIndex_OBJECTID = pTable.Fields.FindField("OBJECTID");int fieldIndex_SHAPE_Length = pTable.Fields.FindField("SHAPE_Length");int fieldIndex_Info = pTable.Fields.FindField("Info");// 字段排序ITableSort pTableSort = new TableSort();pTableSort.Table = pFeatureClass as ITable;pTableSort.Compare = new CustomerSortRole();pTableSort.Fields = "Info";pTableSort.set_Ascending("Info", false);pTableSort.Sort(null);// 要素游标ICursor pCursor = pTableSort.Rows;IRow pRow = pCursor.NextRow();while (pRow != null){DataRow dataRow = dataTable.NewRow();dataRow[0] = pRow.get_Value(fieldIndex_OBJECTID).ToString();dataRow[1] = pRow.get_Value(fieldIndex_SHAPE_Length).ToString();dataRow[2] = pRow.get_Value(fieldIndex_Info).ToString();dataTable.Rows.Add(dataRow);pRow = pCursor.NextRow();}// 释放游标System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);return dataTable;}}// 自定义排序规则public class CustomerSortRole : ITableSortCallBack{public int Compare(object value1, object value2, int FieldIndex, int fieldSortIndex){double a = double.Parse(value1.ToString().Substring(value1.ToString().IndexOf("_") + 1));double b = double.Parse(value2.ToString().Substring(value2.ToString().IndexOf("_") + 1));if (a < b){return -1;}else if (a == b){return 0;}else{return 1;}}}
}

排序结果如下图所示:

ArcEngine实现要素类排序的四种方法相关推荐

  1. 对自定义类实现排序的四种方法

    一.使用LINQ. 二.使用接口IComparable<Product>. 三.使用Sort(Comparison<T> comparison)重载方法. 四.直接使用Orde ...

  2. datatable的数据进行组内排序_Spark实现分组Top-k排序的四种方案(scala语言)

    Spark中实现分组排序(取Top-k)的四种方法. 以求每个学科最受欢迎的老师为例,假设学科下每个老师的主页访问量的多少代表该老师的受欢迎程度. 截取log日志文件中的网址数据的格式为: 数据格式: ...

  3. Struts2教程6:在Action类中获得HttpServletResponse对象的四种方法

    在struts1.x Action类的execute方法中,有四个参数,其中两个就是response和request.而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得H ...

  4. 在Action类中获得HttpServletResponse对象的四种方法

    Struts2:在Action类中获得HttpServletResponse对象的四种方法 在struts1.x Action类的execute方法中,有四个参数,其中两个就是response和req ...

  5. mysql排序的四种方式

    mysql排序的四种方式 第一种,默认排序 第二种,field函数排序 第三种,条件排序 第四种,多重条件排序 第一种,默认排序 按照 order by 字段1 desc/asc, 字段2 desc/ ...

  6. 如何应对云爆发架构?四种方法替你解忧

    [TechTarget中国原创] 虽然大多数CIO喜欢混合云方案,但现实却悄悄遇到了点烦人的小问题--如受美国和欧盟的一些电信业务光纤连接投资不足所累.欢迎来到云爆发架构的地狱式网络体验. 缺乏公有云 ...

  7. python可以实现哪些功能_Python中实现机器学习功能的四种方法介绍

    本篇文章给大家带来的内容是关于Python中实现机器学习功能的四种方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在本文中,我们将介绍从数据集中选择要素的不同方法; 并使用S ...

  8. oracle进行排序,oracle排序的几种方法

    1.创建数据库表 CREATE TABLE USER_INFO ( USERID      VARCHAR2(10 BYTE)                 NOT NULL, USERNAME   ...

  9. java的arraylist_Java ArrayList排序的3种方法

    首页 > 基础教程 > 集合框架 > ArrayList类 Java ArrayList排序的3种方法 1. 使用Collections.sort()方法进行排序 ArrayList ...

最新文章

  1. smarty_modifier_truncate,无或者有md_substr的情况下都能正确截取字符串的php函数,可用于smarty。...
  2. NSAssert的使用
  3. java int.parse_java数据类型转换,parseXXX(String)或valueOf(String)有什么区别?
  4. c语言学习-输入三个数求三个数中的最大数
  5. VMware Workstation安装RedHat Linux 9
  6. /plus/recommend.php sql注入漏洞,代码审计:ThinkPHP框架通杀所有版本的一个SQL注入漏洞详细分析及测试方法 | Seay 渗透 编程 代码审计 网络安全博客...
  7. 在RedHat下安装MPlayer
  8. 微软正式发布 Azure IoT Central
  9. Windows开发——内存读写API
  10. linux pagecache与内存占用
  11. 【心电信号】基于matlab NLM时间序列心电信号去噪【含Matlab源码 1547期】
  12. MVC-Chart_WebGrid 显示漂亮chart
  13. 创新企业如何“跨越鸿沟”?
  14. EPSON-LQ 300K II驱动安装问题
  15. lammps聚合物模拟,“bond missiong”键丢失的一个原因及解决技巧
  16. WACV 2021 论文大盘点-GAN篇
  17. 短视频剪辑APP开发快速开发
  18. 英语句子摘抄——书虫系列
  19. 内存颗粒和闪存颗粒的区别_内存条怎么判断好坏? 内存颗粒的种类及其差别介绍...
  20. INTERN: A New Learning Paradigm Towards General Vision

热门文章

  1. 引导区坏 计算机无法启动,小白告诉你Win10无法正常启动修复引导文件教程
  2. windows nginx出现 was not signaled for 5s的看过来
  3. 儿童发烧、高热,警惕病毒性感冒【程序员爸爸们学着点】
  4. 转帖:经济危机来临时的上海MM生活
  5. xyplorer的完美设置
  6. eclipse:解决 The word is not correctly spelled问题
  7. 计算机上如何使用文件管理器,苹果iphone iFiles文件管理器使用图文教程
  8. 萌新卷妹带你逃出算法无名岛第五站
  9. 如何恢复删除的微信聊天记录?(详细攻略)
  10. 算法证明题 8.9 HITTING SET