foundset in-memory sorting

I’m trying to sort a foundset fs:

function recalculatePositionNumbers( index, foundSet, positionNumberColumnName, formName ) {	
	var 	/** @type {JSFoundSet} */
		fs;
		
	if ( !foundSet ) {
		foundSet = 'foundset';
	}
	fs = forms[ formName ][ foundSet ];

	function sortFunction( r1, r2 ) {
		var o = 0;
		if( r1[ positionNumberColumnName ] < r2[ positionNumberColumnName ] ) {
			o = -1;
		}
		else if( r1[ positionNumberColumnName ] > r2[ positionNumberColumnName ] ) {
			o = 1;
		}
		return o;
	}
	fs.sort( sortFunction );
}

but every time after sorting the foundset fs is empty. What am I doing wrong?

r1 and r2 are records, but what is positionNumberColumnName? If that is what it sounds (a number indicating some column index), then this won’t work. It should be the name of a dataprovider.

patrick:
… what is positionNumberColumnName?

That is the name of the dataprovider which stores the position number. That’s the call:

recalculatePositionNumbers( selectedComIndexBefore, null, 'posnr', controller.getName() );

and positionNumberColumnName is a parameter of the function recalculatePositionNumbers which contains the above code. positionNumberColumnName contains ‘posnr’.

patrick:
If that is what it sounds (a number indicating some column index), then this won’t work. It should be the name of a dataprovider.

It sounds like the column name for the position number. ;)

so you are really saying that:

application.output("before sort: " + fs.getSize());
fs.sort( sortFunction );
application.output("after sort: " + fs.getSize());

that the size before sorting is > 0 and then after sort it is 0 ?

jcompagner:
so you are really saying that:

application.output("before sort: " + fs.getSize());
fs.sort( sortFunction );
application.output("after sort: " + fs.getSize());

that the size before sorting is > 0 and then after sort it is 0 ?

Yes.

before sort: 2

after sort: 0

but only if in the frameworks ‘add’ mode. If in ‘edit’ mode fs.sort( sortFunction ) leaves the foundset intact as long as I don’t create a new record. For example, if I have 3 saved records in the database, go to edit, create a new record, then delete a database saved record and then sort the froundset, then form shows only the 2 database saved records. The new created record is also lost after sort. As a workaround I use:

if ( !identifierColumn ) {
	jsTable = databaseManager.getTable( fs.getDataSource() );
	identifierColumns = jsTable.getRowIdentifierColumnNames();
	identifierColumn = identifierColumns[ 0 ];
}
ds = databaseManager.convertToDataSet( fs, [ positionNumberColumnName, identifierColumn ] );
ds.sort( 1, true );

can you see this in a small example?
so having a foundset with just 2 new records (what kind of pk’s do they have? servoy sequence or db_ident?)

and you you sort that with just new records, always the new records are gone?

Please make a case for that

jcompagner:
can you see this in a small example?
so having a foundset with just 2 new records

2 new records, delete 1 record, sort. → empty

before sort: 1

after sort: 0

jcompagner:
(what kind of pk’s do they have? servoy sequence or db_ident?)

db identity

jcompagner:
and you you sort that with just new records, always the new records are gone?

It seems to be the case. In my test always new records are lost.

jcompagner:
Please make a case for that

SVY-5454: new records are lost after foundset in-memory sorting

It seems that this problem occurs, if it’s a FoxPro DBF table and the “row_ident” in Servoy is declared as “db identity” and in FoxPro the column is declared as “Integer (Autoinc)”. I changed the “row_ident” in Servoy to “servoy seq” and the column in FoxPro DBF to “Integer” and now the foundset is not empty after sort.

before sort: 2

after sort: 2