function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

Apex class to sort years and year ranges giving incorrect output

Tried writing a sortmethod where i was given a a string of comma-delimited years and year ranges,

Expectation is to get a string of comma-delimited years and year ranges,and remove all duplicates and invalid inputs.

My class is not doing it
public class sortYearAndYearRangesString {
  public static List<String> sortSpecialString(String input) {
       system.debug(input);
        List<String> inputList = input.split('');
      system.debug(inputList);
    Map<Integer,String> stringMap = new Map<Integer,String>();
      system.debug(stringMap);
    List<String> output = new List<String>();
    for (Integer i=0; i<inputList.size(); i++) {
        String charac = inputList[i];
        if(!charac.isAlphaNumeric()) {
             system.debug(charac);
            stringMap.put(i,charac);
        }else {
            output.add(charac);
            system.debug(output);
        }
    }
    String finalString =  String.join(output,'');
      system.debug(finalString);
    List<String> resultList = finalString.reverse().split('');
    for( Integer I : stringMap.keySet() ){
        system.debug(I);
        resultList.add(I,stringMap.get(I));
         system.debug(resultList);
        
       
	}
      return resultList;      
       }
Tried validating the solution in Anonymous Apex but no sucess
 
public static void validateSolution() {
 String input = '2017, 2018,2020-2023,1800-1700,2020,20a9,19z5-1990,2025,20261,2013';
 List<Integer> expected = new List<Integer> {2013,2017,2018,2020,2021,2022,2023,2025};
 List<Integer> actual = sortYearAndYearRangesString(input);
 
 System.assertEquals(expected, actual, 'Invalid Results');
 }
}

can someone help to make code changes below,your help is highly appreciated

Regards,

Fiona