@@ -34,6 +34,7 @@ export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistor
34
34
export getAddHistory, getDescription, getLabelDict
35
35
export addVariable!, addFactor!
36
36
export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
37
+ export sortVarNested
37
38
export getVariable, getFactor
38
39
export updateVariable!, updateFactor!
39
40
export deleteVariable!, deleteFactor!
@@ -377,17 +378,58 @@ function sortnestedperm(strs::Vector{<:AbstractString}; delim='_')
377
378
return sp1[sp2]
378
379
end
379
380
380
- # function sortVariableLabels(usl::Vector{Symbol})::Vector{Symbol}
381
- # # x, l = String[], String[]
382
- # # xval = Int[]
383
- # # xstr = String[]
384
- # # xvalnested = String[]
385
- # # xstrnested = String[]
386
- # # canparse1 = true
387
- # # nestedparse1 = true
388
- # # idx = 0
389
- #
390
- # end
381
+ """
382
+ $SIGNATURES
383
+
384
+ Sort a variable list which may have nested structure such as `:x1_2` -- does not sort for alphabetic characters.
385
+ """
386
+ function sortVarNested (vars:: Vector{Symbol} ):: Vector{Symbol}
387
+ # whos nested and first numeric character offset
388
+ sv = string .(vars)
389
+ offsets = getFirstNumericalOffset .(sv)
390
+ masknested = isnestednum .(sv)
391
+ masknotnested = true .⊻ masknested
392
+
393
+ # strip alphabetic characters from front
394
+ msv = sv[masknotnested]
395
+ msvO = offsets[masknotnested]
396
+ nsv = sv[masknested]
397
+ nsvO = offsets[masknested]
398
+
399
+ # do nonnested list separately
400
+ nnreducelist = map ((s,o) -> s[o: end ], msv, msvO)
401
+ nnintlist = parse .(Int, nnreducelist)
402
+ nnp = sortperm (nnintlist)
403
+ nnNums = nnintlist[nnp] # used in mixing later
404
+ nonnested = msv[nnp]
405
+ smsv = vars[masknotnested][nnp]
406
+
407
+ # do nested list separately
408
+ nestedreducelist = map ((s,o) -> s[o: end ], nsv, nsvO)
409
+ nestedp = sortnestedperm (nestedreducelist)
410
+ nesNums = parse .(Int, getindex .(split .(nestedreducelist[nestedp], ' _' ),1 )) # used in mixing later
411
+ nested = nsv[nestedp]
412
+ snsv = vars[masknested][nestedp]
413
+
414
+ # mix back together, pick next sorted item from either pile
415
+ retvars = Vector {Symbol} (undef, length (vars))
416
+ nni = 1
417
+ nesi = 1
418
+ lsmsv = length (smsv)
419
+ lsnsv = length (snsv)
420
+ MAXMAX = 999999999999
421
+ for i in 1 : length (vars)
422
+ # inner ifs to ensure bounds and correct sorting at end of each list
423
+ if (nni<= lsmsv ? nnNums[nni] : MAXMAX) <= (nesi<= lsnsv ? nesNums[nesi] : MAXMAX)
424
+ retvars[i] = smsv[nni]
425
+ nni += 1
426
+ else
427
+ retvars[i] = snsv[nesi]
428
+ nesi += 1
429
+ end
430
+ end
431
+ return retvars
432
+ end
391
433
392
434
393
435
"""
0 commit comments