java - Java Spring MVC - 模型中类的动态属性名称
问题描述
存在简单的解决方案如何(C# . NET):
private void sortData(string param, string type)
{
var propertyInfo = typeof(MusicCatalogueRowClass).GetProperty(param);
if (type == "asc")
{
_Data.rows = _Data.rows.OrderBy(o => propertyInfo.GetValue(o, null)).ToList();
}
else {
_Data.rows = _Data.rows.OrderByDescending(o => propertyInfo.GetValue(o, null)).ToList();
}
saveDataToFile();
}
用Java Spring(没有.NET)制作?我想按动态名称属性对对象列表进行排序...
非常感谢您的任何建议...
解决方案
我的解决方案:
private void sortData(String param, String type)
{
try
{
List<MusicCatalogueRowClass> newData= _Data.getRows();
newData.sort(Comparator.comparing(MusicCatalogueRowClass -> {
try {
return (Comparable) MusicCatalogueRowClass.getClass().getDeclaredField(param).get(MusicCatalogueRowClass);
} catch (Exception e) {
throw new RuntimeException("Ooops", e);
}
}));
if (type.equals("desc")) {
Collections.reverse(newData);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
saveDataToFile();
}
推荐阅读
- android - 值未在 Spinner Android 中正确显示
- typescript - 如何在打字稿中检查类型是否为枚举
- javascript - 如何在 Svelte 上移动道具数组?
- html - 使用一条路径重定向到另一个网站
- python - 如何找到同一年所有分数的平均值(Python)
- android - 使用 android 导航 safeArgs 时的许多参数
- c# - asp.net core登录功能报错“并非所有代码路径都返回值”
- django - 如何更改 moziila_django_oidc 中的 redirect_uri?
- winapi - 在 .code 之外调用汇编函数
- javascript - 延迟加载 Wildfire 评论