You need to sign in to do that
Don't have an account?
fiona gentry
Apex class Not working to get a string of comma-delimited years and year ranges,and remove all duplicates and invalid inputs.
Hi Gurus,
Need help in getting the Apex class to working to output a string of comma-delimited years and year ranges,and remove all duplicates and invalid inputs.
Here is anonymous apex output
Your Help is highly appreciated
Fiona
Need help in getting the Apex class to working to output a string of comma-delimited years and year ranges,and remove all duplicates and invalid inputs.
public class SortYearAndYearRangesString{ public static List<Integer> sortSpecialString(String input){ List<String> inputList = input.split(','); Set<Integer> output = new Set<Integer>(); system.debug('input ' + input); system.debug('inputList ' + inputList); for (String s : inputList){ Set<Integer> tempSet = new Set<Integer>(); s.remove(' '); if (s.contains('-')){ //// break the ranges and fill in years List<String> tempSet2 = s.split('-'); for (String s2 : tempSet2){ try{ ///capture valid integers Integer tempInt = Integer.valueOf(s2); tempSet.add(tempInt); } catch (Exception e){ tempSet.clear(); break; } } System.debug('set ' + tempSet); if (tempSet.size() > 1){ List<Integer> tempList = new List<Integer>(tempSet); tempList.sort (); Integer r = tempList.size() - 1; // iterate through the years for (Integer i = tempList.get(0); i < tempList.get(r); i++){ tempSet.add(i) ; } } } else{ try{ ///capture valid integers Integer tempInt = Integer.valueOf(s); tempSet.add(tempInt); } catch (Exception e){ continue; } } output.addAll(tempSet); } // output is currently set of ints, need to convert to list of integer List<Integer> finalOutput = new List<Integer>(output); finalOutput.sort (); System.debug('finalOutput :' + finalOutput); return finalOutput; }}
Here is anonymous apex output
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.sortSpecialString(input); System.assertEquals(expected, actual, 'Invalid Results'); } }
Your Help is highly appreciated
Fiona