Unable to Sort Date in Columns Using JAVA

Hi Team,

data sorter is not working for Date Column using JAVA, please share help or share Code Sample for the same.

Regards
Bhupender

@FizzFrags,

Thanks for your query.

See the document with example for your reference:
Data Sorting

If you still find any issue. kindly do share your sample code (runnable) with template files (input file (if any) and output file), we will check and help you through.

@Amjad_Sahi

Thanks for reply, but your code is not working as expected or not even populate the same result as we get on excel sorting A to Z or Z to A. i used the same code please check for large Data set to replicate the issue.

Regards
Bhupender

@FizzFrags,

We are very sorry but we need your simulation code and template file to evaluate your issue precisely. Aspose.Cells should mimic MS Excel behavior. We tried using some test files but we could not reproduce the issue. We request you to provide your template file and sample code (runnable) to show the issue, we will check it soon.

Can you please share DataSort code for 1 Lakh rows and i want only sort sheets on the basis of first column order ascending/ descending.

@FizzFrags,
I have tested the following sample code and found it working fine. You may please give it a try and share the feedback. If your issue is not resolved, please share your sample code and template file for our testing. We will analyze it and provide assistance accordingly.

static void Cells_202561() throws Exception
{
	// Instantiate a new Workbook object.
	Workbook workbook = new Workbook();
	for(int i = 0 ; i < 100000; i++)
	{
		Cell cell = workbook.getWorksheets().get(0).getCells().get(i, 0);
		// Getting the style of A1 cell
		Style style = cell.getStyle();

		// Setting the custom display format to show date as "yyyy-mm-dd hh:mm:ss"
		style.setCustom("yyyy-mm-dd hh:mm:ss");

		// Applying the style to A1 cell
		cell.setStyle(style);
		cell.setValue(GenerateRandomDateTime());
	}
	workbook.save("Java_Template.xlsx");//Saved for testing only
	// Get the workbook datasorter object.
	DataSorter sorter = workbook.getDataSorter();

	// Set the first order for datasorter object.
	sorter.setOrder1(SortOrder.ASCENDING);

	// Define the first key.
	sorter.setKey1(0);

	CellArea cellArea = new CellArea();
	cellArea.StartRow = 0;
	cellArea.StartColumn = 0;
	cellArea.EndRow = 99999;
	cellArea.EndColumn = 0;
	sorter.sort(workbook.getWorksheets().get(0).getCells(), cellArea);

	// Save the excel file.
	workbook.save("Java_DataSorting_out.xls");

	// Print message
	System.out.println("Sorting Done Successfully");
}
static Date GenerateRandomDateTime()
{
	 Random r = new Random();
	    long t1 = System.currentTimeMillis() + r.nextInt();
	    long t2 = t1 + 2 * 60 * 1000 + r.nextInt(60 * 1000) + 1;
	    Date d1 = new Date(t1);
	    Date d2 = new Date(t2);
	    return d2;
	    }