【问题标题】:Sort 1D array in relation to ascending sort of 2D array?相对于二维数组的升序排序一维数组?
【发布时间】:2021-05-05 03:39:38
【问题描述】:

我有两个数组,一个是二维数组,另一个是一维数组。我想根据第一个数组对第二个数组进行排序

示例

输入
Long [][] timeArray = {(1,2),(6,8),(3,5)};
String [] nameList = {George, Bill, Harry};

输出
timeArray = {(1,2),(3,5),(6,8)} //Array sorted in ascending order
nameList = {George, Harry, Bill} //Array sorted in relation to timeArray

我可以使用

对二维数组进行排序

Arrays.sort(timeArray, Comparator.comparingLong(a -> a[0]));

但我无法在不使用过于复杂的代码的情况下对它们进行相互排序。

【问题讨论】:

  • Java 有一种称为类的简洁结构,它允许您将名称和时间等内容组合在一个实例中,可以按名称顺序或时间顺序进行排序。
  • 天啊!我就是这么做的。猜我看得太难了? My Answer

标签: java arrays sorting


【解决方案1】:

您需要定义一个自定义比较器,它对 nameList 进行排序但查看 timeArray:

List<String> list = Arrays.asList(nameList);
Comparator relationComparator = (a, b) -> {
  int indexA =list.indexOf(a);
  Int indexB = list.indexOf(b);

  return timeArray[indexA][0] - timeArray[indexB][0];
}

【讨论】:

    【解决方案2】:

    您可以通过将 2 个数组(名称和时间)放在地图中来关联它们,例如:

        public static void main(String[] args) {
            Map<String, Long[]> peopleTimes = new HashMap<>();
            peopleTimes.put("George", new Long[]{1L, 2L});
            peopleTimes.put("Bill", new Long[]{6L, 8L});
            peopleTimes.put("Harry", new Long[]{3L, 5L});
    
            Map<String, Long[]> sortedByTime = peopleTimes.entrySet().stream()
                    .sorted((e1, e2) -> compareTimes(e1.getValue(), e2.getValue()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        }
    
        // You can define your compare logic or use Comparator
        public static int compareTimes(Long[] time1, Long[] time2) {
            if (time1[0] < time2[0]) return -1;
            if (time1[0] > time2[0]) return 1;
            return Long.compare(time1[1], time2[1]);
        }
    

    【讨论】:

      【解决方案3】:

      所以我自己想出了一种排序方法。我将这两个数组复制到自定义类的 ArrayList 中,然后使用 Comparator 对 ArrayList 进行排序。我仍然认为代码是hacky。接受建议/改进。

      public class TimeLog{
          long startTime;
          long endTime;
          String name;
          public TimeLog(long startTime, long endTime, String name){
              super();
              this.startTime = startTime;
              this.endTime = endTime;
              this.name = name;
          }
      }
      

      在主/函数中

      ArrayList<TimeLog> timeLog = new ArrayList<>(); //Initializing the custom ArrayList
      
      //Adding timeArray and nameList into custom ArrayList
      for(int i = 0; i < timeArray.length; i++) {
          timeLog.add(new TimeLog(timeArray[i][0], timeArray[i][1], nameList[i]));
      }
      
      //Sorting using Array.sort and Comparator comparing start times (lambda function)
      timeLog.sort(Comparator.comparingLong(st -> st.startTime));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 2012-04-03
        • 1970-01-01
        • 2013-08-17
        • 2021-05-23
        • 2017-05-25
        相关资源
        最近更新 更多