Sort Numbers correctly in an uitable

2 Ansichten (letzte 30 Tage)
jfaulhab
jfaulhab am 4 Jun. 2018
Kommentiert: jfaulhab am 4 Jun. 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 Kommentar
jfaulhab
jfaulhab am 4 Jun. 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...)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 4 Jun. 2018
Bearbeitet: Stephen23 am 4 Jun. 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 Kommentar
jfaulhab
jfaulhab am 4 Jun. 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 ...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by