Sort months ( with strings ) algorithm

前端 未结 10 1155
深忆病人
深忆病人 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:23

    For something like months, I'd just hard-code the arrays I needed...

    var correctOrdering = {
        english: ["January", "February", "March", ...],
        french: ["Janvier", "Février", "Mars", ...],
        russian: ["Январь", "февраль", "март"],
        ...
    };
    

    It's not like month names are going to change any time soon.

    0 讨论(0)
  • 2021-01-18 04:29

    Create a mapping:

    month_map = {"January":1,
                 "February":2,
                 "March":3,
                 "April":4} # etc..
    

    Use the mapping to compare one month to the other.

    OR

    Most languages/frameworks have objects for handling dates. Create date objects for all the months and compare them using the native (if available) inequality operators or basic sorting functions:

    import datetime
    January  = datetime.date(2010,1,1)
    February = datetime.date(2010,2,1)
    if February < January: print("The world explodes.")
    
    0 讨论(0)
  • 2021-01-18 04:29

    Add a prefix for each month:

    
    Jan -> aJan
    Feb -> bFeb
    ...
    
    

    Sort, then remove the prefix.

    0 讨论(0)
  • 2021-01-18 04:30

    tl;dr

    EnumSet.of( Month.JANUARY , Month.MARCH , Month.OCTOBER , Month.DECEMBER ).toString()
    

    Enum

    If your language provides a powerful enum facility as does Java, define a dozen objects. See Oracle Tutorial.

    java.time.Month

    The java.time classes include the handy Month enum, defining a dozen objects one for each month of the year January-December.

    They are numbered 1-12, and defined in proper order, January to December.

    In your code base, use objects of this enum to replace any use of mere integers or use of name-of-month strings. Using Month objects throughout provides type-safety, ensures valid values, and makes your code more self-documenting.

    In Java, the EnumSet and EnumMap are implementations of Set and Map that are optimized for enum values. They execute very quickly and take very little memory.

    EnumSet<Month> months = EnumSet.of( Month.JANUARY , Month.MARCH , Month.OCTOBER , Month.DECEMBER );
    

    The EnumSet iterates in natural order, the order in which the enum constants are declared. So no need to explicitly sort your collection.

    The class includes a getDisplayName method for generating a localized String of the name of the month. Specify a TextStyle for how long or abbreviated you want the text. And specify a Locale for (a) the human language to use in translation, and (b) the cultural norms to decide issues such as abbreviation, punctuation, and capitalization.

    for( Month month : months ) {
        String output = month.getDisplayName( TextStyle.SHORT_STANDALONE , Locale.CANADA_FRENCH );  // Or Locale.US, Locale.ITALY, etc.
        System.out.println( output );
    }
    
    0 讨论(0)
提交回复
热议问题