Sort the list based on a parameter of the object element

Source: Internet
Author: User
Tags ming

In Java development, we often encounter some problems need to sort the data, then in the use of the list collection, how to set the reference according to a parameter of the object in the collection?

Today for everyone to share a more stupid sort of way:

The data for each list collection will be a class object, and there will be several variables in each class object. Then we'll sort the variables in the object as a reference.

First, the class variables that need to be referenced are taken out and stored in an array that can be compared to the size of the element, where I use the long array. Please refer to the code:

arraylist<order> orders = new arraylist<order> (); long[] condition = new Long[orders.size ()];for (int i = 0; i < Orders.size (); i++) {Condition[i] = Orders.get (i). GetStartTime (). GetTime (); Sortalgorithmutil.selectsort (orders, condition, "ASC"); Sortalgorithmutil.exchangesort (orders, condition, "ASC"); Sortalgorithmutil.insertsort (orders, condition, "ASC");

      Then, create a sort of tool class (emphasis). Please refer to the code:

package com.example.listdelectdemo;import java.util.list;/** *  sorting algorithm class   Xiao Ming's code path  *   *  @author  warmdoc_ANDROID_001 *  */public class  sortalgorithmutil {/** *  Exchange Sorting Method/bubble Sort method  : Compares and exchanges adjacent two elements, nesting for Each loop moves the largest element back by the condition array, The next loop will perform similar operations on the remaining elements.  *  Apply: Initial state is basically orderly, no repetitive element exchange  *  *  @param  <E> *    Generic entity objects in          list  *  *  @param   rawdatas *             the data collection to sort  * @ param condition *             the conditions to sort, Each object from a data collection represents  *  @param  sortType *              sort Type   positive order/reverse  */public static <e> void exchangesort (List <e> rawdatas, long[]&nbsP;condition, string sorttype)  {if  (sorttype.equals ("ASC"))  { //  positive order for  (int i = 1; i < condition.length; i++)  {//  compare two adjacent elements in the condition array, The larger number moves backwards for  (int j = 0; j < condition.length - i; j++)  {if  (condition[j] > condition[j + 1])  {//  swap the position of adjacent two elements swapelement ( rawdatas, condition, j, j + 1);}}}  else if  (sorttype.equals ("desc"))  { //  reverse for  (int i = 1;  i < condition.length; i++)  {//  compare two elements adjacent to the condition array, and the smaller number moves backwards for  (int j  = 0; j < condition.length - i; j++)  {if  (Condition[j] &NBSP;&LT;&NBSP;CONDITION[J&NBSP;+&NBSP;1])  {//  swap position of adjacent two elements swapelement (rawdatas, condition,  j, j + 1);}}}} /** *  selection Sorting method/Direct Sort method  : Nesting for Each loop depending onSorting criteria selects the smallest or largest element from the array element to be sorted, and arranges the combinations in turn.  *  features: Fewer exchanges than bubble sort, faster than bubble sort high  *  *  @param  <E> *    Generic entity objects in          list  *  *  @param   rawdatas *             the data collection to sort  * @ param condition *             the conditions to sort, Each object from a data collection represents  *  @param  sortType *              sort Type   positive order/reverse  */public static <e> void selectsort (list<e > rawdatas, long[] condition, string sorttype)  {if  (Sorttype.equals ("ASC "))  {int record;for  (int i = 1; i < condition.length; i++ )  {record = 0;for  (INT&NBSP;J&NBSP;=&NBSP;1;&NBSP;J&NBSP;&LT;=&NBSP;CONDITION.Length - i; j++)  {if  (Condition[j] > condition[record])  {record  = j;}}   Swap condition.length - i and current minimum element swapelement (rawdatas, condition, condition.length  - i, record);}}  else if  (sorttype.equals ("desc"))  {int record;for  (int i = 1;  i < condition.length; i++)  {record = 0;for  (int j =  1; j <= condition.length - i; j++)  {if  (condition[j]  < condition[record])  {record = j;}}   Swap condition.length - i and current minimum element swapelement (rawdatas, condition, condition.length  - i, record);}}} /** *  Insert Sort method  : Nesting for Each loop adds a new element to the processed element and compares it in the appropriate place each time, creating an ordered array   *  feature: good performance.  *  applies:  *  *  @param when record size is small  <E> *        Generic entity objects in      list  *  *  @param  rawDatas *              sorted data collection  *  @param  condition *              the criteria to sort on, each object from the data set represents a  * @ param sorttype *             sort Type   positive order /Reverse  */public static <e> void insertsort (list<e> rawdatas, long[ ] condition, string sorttype)  {if  (sorttype.equals ("ASC"))  {for  (int i  = 1; i < condition.length; i++)  {for  (int j = 0;  j < i; j++)  {if  (Condition[j] > condition[i])  {//  Swaps x and y two elements swapelement (RAWDATAS,&NBSP;CONDITION,&NBSP;I,&NBSP;J);}}}  else if  (sorttype.equals ("desc"))  {for (int i = 1; i < condition.length; i++)  {for  (int j =  0; j < i; j++)  {if  (Condition[j] < condition[i])  {//   Swap x and y two elements swapelement (RAWDATAS,&NBSP;CONDITION,&NBSP;I,&NBSP;J);}}}} /** *  Interchange the location of the specified element  *  *  @param  <E> *  @param  rawdatas  *  @param  condition *  @param  x *  @param  y */private  Static <e> void swapelement (List<e> rawdatas, long[] condition,  int x, int y)  {long tempC = condition[x];condition[x] =  condition[y];condition[y] = tempc;if  (rawdatas != null)  {E temp =  rawdatas.get (x); Rawdatas.set (X, rawdatas.get (y)); Rawdatas.set (y, temp);}}

Finally, using or printing the list collection that was originally created, the sort effect is implemented.

I have a more general technical level, the code has shortcomings in the hope of advice. Thank you!


This article is from "Xiao Ming's Code Road" blog, please be sure to keep this source http://798056674.blog.51cto.com/9862415/1701095

Sort the list based on a parameter of the object element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.