Sort months ( with strings ) algorithm

前端 未结 10 1287
深忆病人
深忆病人 2021-01-18 03:42

I have this months array:

[\"January\", \"March\", \"December\" , \"October\" ]

And I want to have it sorted like this:

[\"         


        
10条回答
  •  长情又很酷
    2021-01-18 04:19

    Colleagues,

    I see the issue/business problem lasts more than 2 year. I decided to write comparator for sorting months' names (stored as strings) properly. It also holds names of the months for desired locale ============== Comparator =======================

    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    
    /**
     *
     * @author akashtalyan
     */
    public class MonthNamesComparator implements Comparator {
    
        private static Map monthMap = new HashMap();
        private final Locale locale;
    
        public MonthNamesComparator(Locale locale) {
            if (locale == null) {
    
                throw new NullPointerException("MonthNamesComparator cannot accept null value for Locale parameter.");
            }
            List months = new ArrayList(12);
            Calendar cal = Calendar.getInstance(locale);
            SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM", locale);
            this.locale = locale;
            if (!monthMap.containsKey(locale)) {
                for (int i = 0; i < 12; i++) {
                    cal.set(Calendar.MONTH, i);
                    months.add(dateFormat.format(cal.getTime()).toLowerCase());
                }
                monthMap.put(locale , months);
            }
        }
    
        @Override
        public int compare(Object month1, Object month2) {
            List months = monthMap.get(this.locale);
            if (months == null) {
                throw new NullPointerException("MonthNamesComparator cannot perform comparison - internal data is not initialized properly.");
            }
            return (months.indexOf(((String) month1).toLowerCase()) - months.indexOf(((String) month2).toLowerCase()));
    
        }
    }
    

    And simple test class to POC:

    import java.util.Locale;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    /**
     *
     * @author akashtalyan
     */
    public class TestMonths {
        public static void main(String[] args){
            Locale en = Locale.ENGLISH, ru = new Locale("ru","RU");
            String[] monthsToTestEn = new String[] {"FebRUary", "maY", "sepTember", "january", "december"};
            String[] monthsToTestRu = new String[] {"АпреЛь", "январь", "Март", "Август"};
    
            Map map = new TreeMap(new MonthNamesComparator(en));
            int i = 0;
            System.out.println("En Map original:");
            for (String month : monthsToTestEn) {
                System.out.print(month + " ");
                map.put(month, new StringBuilder(String.valueOf(++i)).append(" position in source array").toString());
            }
                System.out.println();
                System.out.println("En Map sorted:");
            for (String month : (Set)map.keySet()) {
                System.out.println(month + " " + map.get(month));
            }
            i = 0;
            map = new TreeMap(new MonthNamesComparator(ru));
            System.out.println("Ru Map original:");
            for (String month : monthsToTestRu) {
                System.out.print(month + " ");
                map.put(month, new StringBuilder(String.valueOf(++i)).append(" position in source array").toString());
            }
                System.out.println();
            System.out.println("Ru Map sorted:");
            for (String month : (Set)map.keySet()) {
                System.out.println(month + " " + map.get(month));
            }
        }
    
    }
    

    Enjoy it, works like a charm.

提交回复
热议问题