sorting input file(each line in first value) (each line in second value) csv in groovy

 // Initialize an ArrayList to store the lines

def lines = []


def file = '''

Header Line

c,3,2342

d,4,234

a,1,55

b,2,33

'''


// Split the input by line and skip the first line (header)

def linesToProcess = file.readLines().drop(1)


// Add the lines to the ArrayList

linesToProcess.each { line ->

    // Check if the line contains a comma before adding

    if (line.contains(',')) {

        lines.add(line)

    }

}


// Sort the ArrayList based on the second value (index 1) of each line

lines.sort { line1, line2 ->

    def value1 = line1.split(',')[1] as Integer

    def value2 = line2.split(',')[1] as Integer

    value1 <=> value2

}


// Print the sorted ArrayList

lines.each { println it }

// sorting in decending 1 value before ,


lines.sort { line1, line2 ->

    def value1 = line1.split(',')[0] as String

    def value2 = line2.split(',')[0] as String

    value2 <=> value1

}


// Print the sorted ArrayList

lines.each { println it }

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024