Sort Numbers correctly in an uitable

2 visualizaciones (últimos 30 días)
jfaulhab
jfaulhab el 4 de Jun. de 2018
Comentada: jfaulhab el 4 de Jun. de 2018
Hey guys,
I want to sort the numbers this way (e.g. 145 129 90 2 -1) inside my uitable htable. Instead the following code sorts the numbers in that way (-1 129 145 2 90). It´s obvious that the program is just sorting after the first numeric value. Has someone got a clue about how to sort these numbers right? Below you will find the current code, which implements the sorting (Credits to Yair Altman).
Many thanks in advance!
% Display the uitable and get its underlying Java object handle
jscrollpane = findjobj(hTable);
jtable = jscrollpane.getViewport.getView;
% Now turn the JIDE sorting on
jtable.setSortable(true);
jtable.setAutoResort(true);
jtable.setMultiColumnSortable(true);
jtable.setPreserveSelectionsAfterSorting(true);
  1 comentario
jfaulhab
jfaulhab el 4 de Jun. de 2018
Basically, I think I Need to change the way of comparing/sorting values in each column... Instead of only sorting after the first numeric value, it should take the whole number into consideration... Btw the underlying datatype of the column data is double. If I look for methods(jtable) I get a list with all possible methods. Still I cannot figure out how to adjust the sorting order (perhaps sth. with Comparator...)

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 4 de Jun. de 2018
Editada: Stephen23 el 4 de Jun. de 2018
I am sure that you could find some Java add-on that provides this functionality, but if you want to sort strings with numeric values then basically you will need to get the table values, convert to numeric (if required), sort, and then apply that sort order to the contents of the table.
If the data are a cell array of strings (char vectors) then one way of doing this would be to download my FEX submission natsortrows:
  1 comentario
jfaulhab
jfaulhab el 4 de Jun. de 2018
Thank you for the Response. This would actually Change the sorting of the data in my uitable from the beginning on... But I want to be able to see the data unsorted, and then by clicking at the column Header to sort the data according highest/lowest numbers... One opportunity to implement your code could be to check if the Header gets clicked, and then to get in which way the data gets sorted and then to use this Information, sort the data with your code and update the uitable... Since I don´t know how to get the "click" Information I think implementing your advice would take more time ...

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by