Sorting in Mobile app

Hello,

Lists on mobile side are sorted by table Ids (by default). Is there a way to change the field on which I would like to sort ?

By example, I would like to sort the companies list by company_name asc but it seems to not work in mobile.

Thank you.

On mobile you don’t have a real database that understands a SQL “Order By” clause. You can sort the foundset by using a sort function. Something like

function fsSort(record1, record2) {
	var result = 0;
	if (record1.company_name > record2.company_name) {
		result = 1;
	} else if (record1.company_name < record2.company_name) {
		result = -1;
	}
	return result;
}

fs.sort(fsSort);

Hello patrick,

Thank you very much for your answer.

This solution doesn’t work for me, but I think this is because my Mobile form is based on the “entites” table. To access my field, I have to write “entites_to_companies.company_name”.

And I think that this method of sorting doesn’t work with related fields, right ?

Why shouldn’t this work:

var record = foundset.getSelectedRecord(); // your entity
var fs = record.entites_to_companies; // the foundset you want to sort

fs.sort(fsSort);

We have to do this on the mobile side ? Sorry, I can not make it work :(

Can you show the code that you are using now?

I don’t use relation anymore, but just a table companies, so in theory it is very simple. I use the code that you gave me first. the field’s name is “comp_name”, so in my mobile side I have :

function fsSort(record1, record2) {
   var result = 0;
   if (record1.comp_name > record2.comp_name) {
      result = 1;
   } else if (record1.comp_name < record2.comp_name) {
      result = -1;
   }
   return result;
}

and in my onShow() method :
fs.sort(fsSort);

hm. Not sure if that is too late. What if you do this outside of the form you want to show sorted and then say forms.xy.controller.showRecord(sortedFs) to show it?

I don’t success to make it work … “The property controller is private”.

Nobody else has a solution ?

I think I will wait the final version of Servoy 7 to try again. Thank you so much for your answers.

that means that you accessed the “controller” property of a form that has that controller encapsulated (see the forms properties)
You can change this to public

But where did you access the controller property in this sort problem?

Ok I changed the encapsultation but it doesn’t work. The problem is that I need to make a sort on the FirstForm of my solution (companies). In this case, where it is better to call the forms.companies.controller.showRecord(sortedFs)

Johan, for you, is it possible to sort a list in a Mobile app ? if yes, how ?

that should all just work
in companies:

foundset.sort(mysortfunction)

that should sort and display the order in a form. If you have a solution where you don’t see that, create a case with that solution.

ofcourse

var justafoundset = xxxxx;
justafoundset.sorT(mysortfunction)
controller.showRecords(justafoundset)

should also just work

Sorting a foundset via a function seems cool, but is something I haven’t heard of before… Is this Mobile only, or can we do this anywhere?

jgarfield:
Sorting a foundset via a function seems cool, but is something I haven’t heard of before… Is this Mobile only, or can we do this anywhere?

That can be done anywhere, check the sample code for JSFoundset.sort(function).
I think this is possible since 6.0.

Finally, I understood why it didn’t work !
Sage CRM add some space characters (" ") in the field values of the database … That is why the sorting wasn’t working. I had to trim the values in the ws_read, and now it works like a charm.
Thank you for your answers !

Joas:
I think this is possible since 6.0.

That would do it…we’re still transitioning to 6 so I hadn’t seen that yet. Thanks!