根據類中的某個屬性對類進行排序

2021-07-09 08:50:38 字數 546 閱讀 9194

有時候,我們會有這樣的需求:通過制定的類中的元素,對多個例項物件進行排列顯示。

那麼,今天我為大家寫了這樣乙個類,以便使用

話不多說,貼上原始碼

public class listsort).tostring().compareto(m1.invoke(((e) a), new  object{}).tostring());

} else ).tostring().compareto(m2.invoke(((e) b), new object{}).tostring());

} } catch (nosuchmethodexception ne) catch (illegalargumentexception e) catch (illegalacces***ception e) catch (invocationtargetexception e)

return ret;

} });

}}

好了 ,我是用的反射的原理來進行比較的 ,如果有更好的方法,歡迎**學習。

對List集合中物件的某個屬性進行排序

通過讓實體類實現comparable介面,並重寫compareto方法。lombok自動生成getset及tostring data public class entity implements comparable override public int compareto entity o el...

根據屬性值對Array進行排序

trees new array for var i int 0 i 500 i trees.sorton z array.numeric array.descending for i 0 i 500 i array.sort comparefunction sortoptions 其引數定義如下 s...

對list根據某個字段進行排序

今天敲 遇到乙個問題 需要對list的某個字段進行排序,而且需要根據自己定義的順序進行排序 我的需求中根據index實體類的indextype欄位進行排序,順序為 b,d,c,a string 我的indextype欄位為string型別,陣列中為我自定義的順序 用的常量 new fixedorde...