为了账号安全,请及时绑定邮箱和手机立即绑定

根据字段对自定义类的实例数组进行排序

根据字段对自定义类的实例数组进行排序

凤凰求蛊 2023-05-24 16:30:38
我现在在一个项目中遇到了一个问题,我只是为了了解更多关于 java 的信息,所以我有一个 txt 文件,它有奇迹,我必须阅读它,然后将它的不同信息存储在一个对象中( name country, heightdepth, accessibility, hostility, kmfromlondon, temperature, wondervalue and a fact about the wonder), then I have to sorting by the hostility rate 并输出所有奇迹这是奇迹的txt文件:Amazonia,Brazil,60,10,10,8100,30,4,The Amazon rainforest occupies some 5.5 million square kilometres of South America.Iguassu Falls,Brazil|Argentina,82,11,50,10064,25,4,Two hundred and seventy waterfalls flow along nearly 3 kilometres of rivers.Niagra Falls,USA|Canada,50,18,69,5804,20,4,Adventurous tourists can take a cruise on the river below into the falls' mist.Giant's Causeway,Northern Ireland,12,19,17,592,15,2,Legend has it that is is a former 'walkway' to Scotland.Great Barrier Reef,Australia,60,10,10,15292,28,4,Over 30 species of whale and 15000 species of fish live here.Mount Everest,Tibet|Nepal,8840,7,95,7424,-10,4,The highest mountain on Earth is located in the Himalayas.Mount Vesuvius,Italy,1281,18,95,1630,30,1,The only volcano in mainland Europe to have erupted in the last 100 years.Old Faithful,USA,55,14,65,7432,118,2,Over half of the world's geysers are located in Yellowstone national park.Sahara Desert,African Union,3445,14,84,3800,35,3,Covers part of North Africa and is almost the same size as the USA.Great Rift Valley,African Union,1470,14,12,5887,25,4,The valley was formed by activity between tectonic plates 35 million years ago.Gobi Desert,Mongolia|China,2700,9,84,7279,30,3,The desert has been the location of many fossil finds.Ngorongoro Crater,Tanzania,610,14,19,5804,30,2,Home to almost 25000 animals including masses of flamingoes around Lake Magadi.Perito Morena Glacier,Argentina,60,9,82,13230,-5,3,The 5 kilometre wide glacier is well known for its process of rupturing.
查看完整描述

1 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

在 Java 中定义自定义排序的常规方法是通过Comparator实例。该对象可以插入到 egArrays或的排序例程中Collections

例如,在 Java 8 中,您可以像这样对数组进行排序:

Arrays.sort(wonderArray, Comparator.comparingInt(wonder -> wonder.hospitality));

在 Java 7 或更早版本中,您可以使用匿名类来完成此操作:

Arrays.sort(wonderArray, new Comparator<Wonder>() {

  @Override public int compareTo(Wonder w1, Wonder w2) {

    return Integer.compare(w1.hospitality, w2.hospitality);

  }

});


查看完整回答
反对 回复 2023-05-24
  • 1 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信