问题描述
现在table里面有四组数,排序的要求是先对里面的otdkKbkbn进行升序排序,如果有相同的则比较htStbi(升序),如果htStbi里出现相同则比较ysJunni(升序),同样的如果ysJunni里出现相同则比较srNo(升序),最后得到排序后的tablepublic class test {public static void main(String args[]) {InnerDTO workDTO = new InnerDTO();workDTO.getHurryJchCountTbl()[0].setOtdkKbkbn("2");workDTO.getHurryJchCountTbl()[1].setOtdkKbkbn("1");workDTO.getHurryJchCountTbl()[2].setOtdkKbkbn("1");workDTO.getHurryJchCountTbl()[3].setOtdkKbkbn("1");workDTO.getHurryJchCountTbl()[4].setOtdkKbkbn("2");workDTO.getHurryJchCountTbl()[0].setYsJunni("1");workDTO.getHurryJchCountTbl()[1].setYsJunni("2");workDTO.getHurryJchCountTbl()[2].setYsJunni("1");workDTO.getHurryJchCountTbl()[3].setYsJunni("1");workDTO.getHurryJchCountTbl()[4].setYsJunni("2");workDTO.getHurryJchCountTbl()[0].setHtStbi("2");workDTO.getHurryJchCountTbl()[1].setHtStbi("3");workDTO.getHurryJchCountTbl()[2].setHtStbi("2");workDTO.getHurryJchCountTbl()[3].setHtStbi("2");workDTO.getHurryJchCountTbl()[4].setHtStbi("4");workDTO.getHurryJchCountTbl()[0].setSrNo("3");workDTO.getHurryJchCountTbl()[1].setSrNo("2");workDTO.getHurryJchCountTbl()[2].setSrNo("3");workDTO.getHurryJchCountTbl()[3].setSrNo("2");workDTO.getHurryJchCountTbl()[4].setSrNo("1");//-------------------------------------------//sort的处理方法//hurryJchCountTbl sort 前//OtdkKbkbn YsJunni HtStbi SrNo//hurryJchCountTbl[0]2123//hurryJchCountTbl[1]1232//hurryJchCountTbl[2]1123//hurryJchCountTbl[3]1122//hurryJchCountTbl[4]2241////hurryJchCountTbl sort 后//hurryJchCountTbl[0]1122//hurryJchCountTbl[1]1123//hurryJchCountTbl[2]1232//hurryJchCountTbl[3]2123//hurryJchCountTbl[4]2241//------------------------------------------}private static final class InnerDTO {//Default constructorprivate InnerDTO() {HurryJchCountTbl[] checkHurryJchCountTbl = getHurryJchCountTbl();for (int i = 0; i < checkHurryJchCountTbl.length; i++) {checkHurryJchCountTbl[i] = new HurryJchCountTbl(); }}//hurryJchCountTblprivate HurryJchCountTbl[] hurryJchCountTbl = new HurryJchCountTbl[5];//TBLpublic HurryJchCountTbl[] getHurryJchCountTbl() {return hurryJchCountTbl;}public void setHurryJchCountTbl(HurryJchCountTbl[] hurryJchCountTbl) {this.hurryJchCountTbl = hurryJchCountTbl;}}//tblprivate static final class HurryJchCountTbl {private String srNo;private String ysJunni; private String otdkKbkbn; private String htStbi; public String getSrNo() {return srNo;}public void setSrNo(String srNo) {this.srNo = srNo;}public String getYsJunni() {return ysJunni;}public void setYsJunni(String ysJunni) {this.ysJunni = ysJunni;} public String getOtdkKbkbn() { return this.otdkKbkbn; } public void setOtdkKbkbn(String otdkKbkbn) { this.otdkKbkbn = otdkKbkbn; } public String getHtStbi() { return this.htStbi; } public void setHtStbi(String htStbi) { this.htStbi = htStbi; }}}
解决方案
可以试试以下代码:for(int i =0 ; i<workDTO.getHurryJchCountTbl().length ;i++){ HurryJchCountTbl min = workDTO.getHurryJchCountTbl()[i]; int minN = i; for(int j = i+1;j<workDTO.getHurryJchCountTbl().length;j++){ HurryJchCountTbl ls = workDTO.getHurryJchCountTbl()[j]; int k = min.otdkKbkbn.compareTo(ls.otdkKbkbn); if(k>0){ minN = j; }else if(k==0){ int htStbi = min.htStbi.compareTo(ls.htStbi); if(htStbi>0){ minN = j; }else if(htStbi == 0){ int ysJunni = min.ysJunni.compareTo(ls.ysJunni); if(ysJunni>0){ minN = j; }else if(ysJunni == 0){ int srNo = min.srNo.compareTo(ls.srNo); if(srNo>0){ minN = j; } } } } } HurryJchCountTbl c = workDTO.getHurryJchCountTbl()[i]; workDTO.getHurryJchCountTbl()[i] = workDTO.getHurryJchCountTbl()[minN]; workDTO.getHurryJchCountTbl()[minN] = c; }