java - Sorting int field of the wrapper class spring -


i have navigation class dynamically creating navigation having 2 tables folder(it directory contains files) , content(it files or pages render content on public site). have created navigation class in having wrapper class merging fields of content folder. have tried using @orderby , @ordercolumn came know work collections.

list<folder> folder = folderrepository.findallbynavdepthlessthanorderbynavdepthasc(3);

here sorting navdepth(this column belongs folder entity) want sort navorder(this column belongs content entity)

@service public class navigationservice {      @qualifier("jdbcmysql")     private jdbctemplate jdbctemplate;     private folderrepository folderrepository;     private folderservice folderservice;      @autowired     public navigationservice(jdbctemplate jdbctemplate,              folderrepository folderrepository,             folderservice folderservice) {         this.jdbctemplate = jdbctemplate;         this.folderrepository = folderrepository;         this.folderservice = folderservice;     }      @transactional(propagation=propagation.required, readonly=false)     public map<string, navigationitem> navigationitems() {         // todo: // cross cutting aop springs         // todo: http://docs.spring.io/spring/docs/4.2.0.build-snapshot/spring-framework-reference/htmlsingle/#aop         list<folder> folder = folderrepository.findallbynavdepthlessthanorderbynavdepthasc(3);         // list<folder> folder = folderservice.navigation();         map<string, navigationitem> navitems = new linkedhashmap<string, navigationservice.navigationitem>();         (int = 0; < folder.size(); i++) {             navigationitem ni = new navigationitem();             ni.setnavdepth((int) (folder.get(i).getnavdepth()));             ni.setfilenamepath(folder.get(i).getdirectorypath());             ni.setfilepath(folder.get(i).getdirectorypath());             ni.setchildren(folder.get(i).getcontent());             (int k = 0; k < folder.size(); k++) {                 if(folder.get(i).getid() == folder.get(k).getparentid()) {                         ni.addsubfolder(folder.get(k));                         system.out.println(folder.get(i).gettitle());                         system.out.println(folder.get(k));                         system.out.println("---!!!!!!________----------!!!!!!!!");                 }             }             navitems.put(folder.get(i).gettitle(), ni);         }         return navitems;     }      public class navigationitem {         private long id;         private long parentid;         private string title;         private string filename;         private string filenamepath;         private int navdepth;         private int navorder;         private string parentfilename;         private string filepath;         private string foldername;         @ordercolumn(name="navorder asc")         private list<content> children = new arraylist();         private arraylist<folder> subfolder = new arraylist();           public void setsubfolder(arraylist<folder> subfolder) {             this.subfolder = subfolder;         }         public string getfilepath() {             return filepath;         }         public void setfilepath(string filepath) {             this.filepath = filepath;         }         public string getfoldername() {             return foldername;         }         public void setfoldername(string foldername) {             this.foldername = foldername;         }         public arraylist<folder> getsubfolder() {             return subfolder;         }         public void addsubfolder(folder subfolder) {             this.subfolder.add(subfolder);         }         public void setchildren(list<content> list) {             this.children = list;         }         public long getid() {             return id;         }         public void setid(long id) {             this.id = id;         }         public long getparentid() {             return parentid;         }         public void setparentid(long parentid) {             this.parentid = parentid;         }         public string gettitle() {             return title;         }         public void settitle(string title) {             this.title = title;         }         public string getfilename() {             return filename;         }         public void setfilename(string filename) {             this.filename = filename;         }         public string getfilenamepath() {             return filenamepath;         }         public void setfilenamepath(string filenamepath) {             this.filenamepath = filenamepath;         }         public long getnavdepth() {             return navdepth;         }         public void setnavdepth(int navdepth) {             this.navdepth = navdepth;         }         public long getnavorder() {             return navorder;         }         public void setnavorder(int navorder) {             this.navorder = navorder;         }         public string getparentfilename() {             return parentfilename;         }         public void setparentfilename(string parentfilename) {             this.parentfilename = parentfilename;         }         public list<content> getchildren() {             return children;         }         public void addchild(content child) {             children.add(child);         }         public string getfilepath() {             return filepath;         }         public void setfilepath(string filepath) {             this.filepath = filepath;         }      } } 

use comparator<navigationitem> , pass collections.sort() or similar methods.

the comparator might this:

class navcomparator implements comparator<navigationitem> {   int specialvaluenochildren = -1; //assuming nav_order 0 or greater          int compare(navigationitem o1, navigationitem o2) {     int max1 = getmaxnavorder( o1 );     int max2 = getmaxnavorder( o2 );     return integer.compare( max1, max2 );   }    int getmaxnavorder( navigationitem ni ) {     int max = specialvaluenochildren;     for( content child : ni.getchildren() ) {       max = math.max(max, child.getnavorder());     }      return max;   } } 

here maximum nav order of children selected -1 being special case of no children. items compared respective children's maximum nav order.

if need different order change accordingly, e.g. reversing max1 , max2 or getting lowest nav order of children etc.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -