list - Most efficient way to sort by multiple fields in Java -


i'm working on project have table in memory, example:

field1    field2    field3    field4    field5 somevalue somevalue somevalue somevalue somevalue somevalue somevalue somevalue somevalue somevalue     .     .      .     .     v 

what best way write function allow user specify non-predetermined permutation of fields sort? (ex./ sort field2, field1, field5). best method thought of far recursive function, gets ugly pretty quick, creating , managing 'subgroups' of rows within each column. can point me in more efficient direction before commit convoluted approach?

by way, table stored list of arrays.

the key thing realize here search algorithm stays same. thing changes actual comparison function.

i recommend using standard sort in arrays or collections class, writing own custom comparable class or comparator, compare method use data user supplies. kind of interesting - i've ever written comparable classes use fixed data, use case them.

all need write function

public int compare(row row1, row row2) {     // iterate on columns *in order of user-supplied priority*     // return appropriately } 

here's documentation on comparable class.

and comparator.

which 1 need depends on you're implementing, forget details should straightforward.

the key #2 thing here can instantiate columnprioritiescomparable class before priorities set. e.g.

class colpricomp implements comparable<colpricomp> {     private volatile int[] priorities; // possibly static, or obtained other way,     // there's 1 shared among classes      @override     public int compareto(colpricomp other) {         // use "priorities" comparison     }      public void setpriorities(int[] newpriorities) {         this.priorities = newpriorities;     } } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -