⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webresultssorter.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
/**
 * 
 */
package com.esri.solutions.jitk.web.data.results;

import java.util.List;

import org.apache.log4j.Logger;

import com.esri.adf.web.data.results.ResultNode;


/**
 * @author vlad2928
 *
 */
public class WebResultsSorter {
	
	private static Logger _logger = Logger.getLogger(WebResultsSorter.class.getName());
	
	private String sortByColumnName = "";
    private boolean sortAscending = true;
    
    public ResultNode sort(ResultNode data, String sortByColumnName, boolean sortAscending) {
		
		this.sortByColumnName = sortByColumnName;
		this.sortAscending = sortAscending;
		
		if(data != null && this.sortByColumnName != null && this.sortByColumnName.length() > 0) {
			try {
				List<ResultNode> rows = data.getChildren();
				if(rows != null && rows.size() > 1) {
					quickSort(rows, 0, rows.size() - 1);
				}
			} catch(Exception e) {
				_logger.warn(e.toString());
			}
		}
		
		return data;
	}
	
	private void quickSort(List<ResultNode> rows, int left, int right) throws ClassCastException {
		int i = left, j = right;
		ResultNode pivotNode = rows.get(new Double(Math.ceil((right+left) / 2)).intValue());
		
		while(true) {
			
			if(!isValidIndex(rows, i) || !isValidIndex(rows, j)) break;
			
			while(isInOrder(rows.get(i), pivotNode)) i++;
			while(isInOrder(pivotNode, rows.get(j))) j--;
			
			if(i <= j) {
				if(!isInOrder(rows.get(i), rows.get(j))) {
					swap(rows, i, j);
				} 
				i++; j--;
				
			} else break;
		}
	    
		if(left < j) {
			quickSort(rows, left, j);
		}
	    
		if(i < right) {
			quickSort(rows, i, right);
		}
	}
	
	@SuppressWarnings("unchecked")
	private boolean isInOrder(ResultNode node1, ResultNode node2) throws ClassCastException {
		
		Object o1 = node1.getDetails().get(sortByColumnName);
		Object o2 = node2.getDetails().get(sortByColumnName);
		
		if(o1 instanceof Comparable && o2 instanceof Comparable) {
			if(sortAscending) {
				return (((Comparable)o1).compareTo((Comparable)o2) < 0);
			} else {
				return (((Comparable)o1).compareTo((Comparable)o2) > 0);
			}	
		}
		
		throw new java.lang.ClassCastException("Objects " + o1.getClass().getName() + " and " + o2.getClass().getName() + " are not comparable.");
	}
	
	private boolean isValidIndex(List<?> list, int i) {
		return (i >= 0 && i < list.size());
	}
	
	private void swap(List<ResultNode> list, int i1, int i2) {
		
		if(i1 != i2 && isValidIndex(list, i1) && isValidIndex(list, i2)) {
			
			ResultNode r1 = list.get(i1);
			ResultNode r2 = list.get(i2);
		
			list.remove(r1);
			list.remove(r2);
			
			list.add(i1, r2);
			list.add(i2, r1);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -