Skip to content

Commit 621d492

Browse files
committed
sort win32 ports in reverse order
1 parent 72f450f commit 621d492

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

inst/private/__arduino_serialportlist__.m

+24-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
endfor
2626
elseif isunix() && !ispc()
2727
list = port_sort(ports, @unix_port_compare);
28+
elseif !isunix() && ispc()
29+
list = port_sort(ports, @win32_port_compare);
2830
else
2931
list = ports;
3032
endif
@@ -67,10 +69,29 @@
6769
endif
6870
endfunction
6971

72+
function res = win32_port_compare(s1, s2)
73+
# we want to priortise comports - assuming
74+
# that comports more likely to arduinos
75+
# will be towards end of the list
76+
77+
s1_p = strncmp(s1, "COM", 3);
78+
s2_p = strncmp(s2, "COM", 3);
79+
if s1_p && s2_p
80+
# sort numerically
81+
s1_p = sscanf(s1, "COM%d");
82+
s2_p = sscanf(s2, "COM%d");
83+
# reverse order
84+
res = s2_p - s1_p;
85+
else
86+
res = c_strcmp(s2, s1);
87+
endif
88+
endfunction
89+
7090
function slist = port_sort(ulist, sort_method=@c_strcmp)
7191
# crappy bubble sort for now
7292
sorted = false;
73-
while sorted == false
93+
max_try = length(ulist) * length(ulist);
94+
while sorted == false && max_try > 0
7495
sorted = true;
7596
for idx=1:length(ulist)-1
7697
cmp = sort_method(ulist{idx}, ulist{idx+1});
@@ -82,6 +103,8 @@
82103
ulist{idx+1} = t;
83104
sorted = false;
84105
endif
106+
# fail safe in case we get given some function that means we can never sort
107+
max_try = max_try - 1;
85108
endfor
86109
endwhile
87110
slist = ulist;

0 commit comments

Comments
 (0)