简体   繁体   English

如何在Java或Groovy中对时间字符串列表进行排序

[英]How to sort a list of time strings in Java or Groovy

I have a list of strings, each one contains time like this: 我有一个字符串列表,每个字符串都包含这样的时间:

"03:00 AM", "12:30 PM", "16:15" “03:00 AM”,“12:30 PM”,“16:15”

I need to sort them in order: put "am" times first and compare hours, not just first digits in a string. 我需要按顺序对它们进行排序:首先放“am”次并比较小时数,而不仅仅是字符串中的第一个数字。

This works for groovy, with all cases you gave: 这适用于groovy,包括你提供的所有情况:

List dates = [ "16:15", "12:30 PM", "07:00", "03:00 AM" ]

dates.sort { d ->
  [ 'h:mm a', 'H:mm' ].findResult { fmt ->
    try {
      Date.parse( fmt, d ).time
    } catch( e ) {}
  }
}

It basically tries with AM/PM and then tries without it if that fails 它基本上尝试使用AM / PM,如果失败则尝试不使用它

All of Java's sorting machinery -- for example, the Collections.sort() methods -- work with (or can work with) a comparator -- an instance of a class you write yourself that implements java.util.Comparator . 所有Java的排序机制 - 例如, Collections.sort()方法 - 与比较器一起工作(或可以使用) - 比较器 - 您自己编写的实现java.util.Comparator的类的实例。 That interface has a compare() method, and you can implement it to do any kind of comparison you want. 该接口有一个compare()方法,您可以实现它以进行任何类型的比较。 That's what you'll need to do here. 这就是你需要做的。

Use the SimpleDateFormat class to determine the time which represents each string and then compare that numeric value when sorting. 使用SimpleDateFormat类确定表示每个字符串的时间,然后在排序时比较该数值。 It looks like the time strings you're dealing with can have different kinds of formats, so I've edited the answer to support strings of format 03:00AM or 16:23 看起来你正在处理的时间字符串可以有不同的格式,所以我编辑了答案以支持格式为03:00AM16:23字符串

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeStringComparator implements Comparator<String>{
   private DateFormat primaryFormat = new SimpleDateFormat("h:mm a");
   private DateFormat secondaryFormat = new SimpleDateFormat("H:mm");

   @Override
   public int compare(String time1, String time2){
       return timeInMillis(time1) - timeInMillis(time2);
   }

   public int timeInMillis(String time){
       return timeInMillis(time, primaryFormat);
   }

   private int timeInMillis(String time, DateFormat format) {
        // you may need more advanced logic here when parsing the time if some times have am/pm and others don't.
       try{
            Date date = format.parse(time);
            return (int)date.getTime();
       }catch(ParseException e){
           if(format != secondaryFormat){
               return timeInMillis(time, secondaryFormat);
           }else{
               throw e;
           }
       }
    }

   public static void main(String[] args){
       List<String> times = Arrays.asList(new String[]{"03:00 AM", "12:30 PM", "16:15"});
       Collections.sort(times, new TimeStringComparator());
       System.out.println(times);
   }

}

As @Ernest said, you need to use a Comparator. 正如@Ernest所说,你需要使用比较器。 You can do something like this with your list: 您可以使用列表执行以下操作:

List<String> times;
Collections.sort(times, new Comparator<String>() { 
     @Override 
     public int compare(String time1, String time2) { 

        // Sorting implementation here
     } 
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM