/*
sort criteria for window list:
open windows first, then alpha by title
10313
function windowCmp(tabWindowA, tabWindowB) {
  // focused window very first:
  var fA = tabWindowA.focused;
  var fB = tabWindowB.focused;
  if (fA !== fB) {
    if (fA) {
      return -1;
    }
    return 1;
  }

  // open windows first:
  if (tabWindowA.open !== tabWindowB.open) {
    if (tabWindowA.open) {
      return -1;
    }
    return 1;
  }

  var tA = tabWindowA.title;
  var tB = tabWindowB.title;
  return tA.localeCompare(tB);
}
444660
private int getCurrentWindowIndex()
    {
        long currentTime = System.currentTimeMillis();
        long currentWindow =((currentTime - startTime) / windowDurationInMs);
        return (int) currentWindow%(numKeys/windowSize);
    }
1302494
@When("^I sort elements in '(.+?)' by '(.+?)' criteria in '(.+?)' order$")
    public void sortElements(String envVar, String criteria, String order) {

        String value = ThreadProperty.get(envVar);
        JsonArray jsonArr = JsonValue.readHjson(value).asArray();

        List<JsonValue> jsonValues = new ArrayList<JsonValue>();
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.get(i));
        }

        Comparator<JsonValue> comparator;
        switch (criteria) {
            case "alphabetical":
                commonspec.getLogger().debug("Alphabetical criteria selected.");
                comparator = new Comparator<JsonValue>() {
                    public int compare(JsonValue json1, JsonValue json2) {
                        int res = String.CASE_INSENSITIVE_ORDER.compare(json1.toString(), json2.toString());
                        if (res == 0) {
                            res = json1.toString().compareTo(json2.toString());
                        }
                        return res;
                    }
                };
                break;
            default:
                commonspec.getLogger().debug("No criteria selected.");
                comparator = null;
        }

        if ("ascending".equals(order)) {
            Collections.sort(jsonValues, comparator);
        } else {
            Collections.sort(jsonValues, comparator.reversed());
        }

        ThreadProperty.set(envVar, jsonValues.toString());
    }
1334145
function _sortByPlatform(a, b) {
        var a1 = (a.platform) ? 1 : 0,
            b1 = (b.platform) ? 1 : 0;
        return b1 - a1;
    }
1051674
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}
1289434
function(sortClause) {

    if (!this._alreadyInitiallyExpandedCriteria) {
      this._wlQueryInfo.criteria = expandWhereShorthand(this._wlQueryInfo.criteria);
      this._alreadyInitiallyExpandedCriteria = true;
    }//>-

    this._wlQueryInfo.criteria.sort = sortClause;

    return this;
  }
111296
private void doMoveSelectionInList(int moveBy)
	{
		PlayListEntry [] selected = playList.getSelectedEntries();
		if (selected==null || selected.length == 0)
		{
			playList.setSelectedElement(0);
			doMakeIndexVisible(0);
		}
		else
		{
			int index = selected[0].getIndexInPlaylist();
			index += moveBy;
			if (index<0) index = 0;
			else
			if (index>=playList.size()) index = playList.size() - 1;
			playList.setSelectedElement(index);
			if (moveBy <0) index-=PLUS_LINES_VISABLE; else index+=PLUS_LINES_VISABLE;
			doMakeIndexVisible(index);
		}
	}
994761
private static void sortMatchList(List<Match> list) {
    if (list != null) {
      // light sorting on start position
      Collections.sort(list, (Match m1,
          Match m2) -> (Integer.compare(m1.startPosition, m2.startPosition)));
    }
  }
590493
private void sortByName() {
		this.sresList.setComparator(new ViewerComparator() {
			@Override
			public int compare(Viewer viewer, Object e1, Object e2) {
				if ((e1 instanceof ISREInstall) && (e2 instanceof ISREInstall)) {
					final ISREInstall left = (ISREInstall) e1;
					final ISREInstall right = (ISREInstall) e2;
					return left.getName().compareToIgnoreCase(right.getName());
				}
				return super.compare(viewer, e1, e2);
			}

			@Override
			public boolean isSorterProperty(Object element, String property) {
				return true;
			}
		});
		this.sortColumn = Column.NAME;
	}
137665
def sortBy(self, *args):
        ''''''
        criteria = ['seeds', 'size']
        for k in args:
            if k in criteria:
                self.results = sorted(
                    self.results, key=attrgetter(k), reverse=True)
        return self
1310167
function orderAndSort(sortConf) {
    return {
        sortFieldName: sortConf.sortBy,
        sortDesc: sortConf.sortAsc === undefined ? false : !sortConf.sortAsc
    };
}
671687
private void selectionSort(final T[] array, int start, int end, Comparator<T> lt) {
    int i;
    int j;
    int bestI;
    T tmp;
    for (i = start; i < end; i++) {
      bestI = i;
      for (j = i + 1; j < end; j++) {
        if (lt.compare(array[j], array[bestI]) < 0)
          bestI = j;
      }
      tmp = array[i];
      array[i] = array[bestI];
      array[bestI] = tmp;
    }
  }
1024086
function removeOpenWindowState(tabWindow) {
  var savedItems = tabWindow.tabItems.filter(function (ti) {
    return ti.saved;
  });
  var resetSavedItems = savedItems.map(resetSavedItem);

  return tabWindow.remove('open').remove('openWindowId').remove('focused').set('tabItems', resetSavedItems);
}
734119
function doGuessOrdinal(
    data, sourceFormat, seriesLayoutBy, dimensionsDefine, startIndex, dimIndex
) {
    var result;
    // Experience value.
    var maxLoop = 5;

    if (isTypedArray(data)) {
        return false;
    }

    // When sourceType is 'objectRows' or 'keyedColumns', dimensionsDefine
    // always exists in source.
    var dimName;
    if (dimensionsDefine) {
        dimName = dimensionsDefine[dimIndex];
        dimName = isObject(dimName) ? dimName.name : dimName;
    }

    if (sourceFormat === SOURCE_FORMAT_ARRAY_ROWS) {
        if (seriesLayoutBy === SERIES_LAYOUT_BY_ROW) {
            var sample = data[dimIndex];
            for (var i = 0; i < (sample || []).length && i < maxLoop; i++) {
                if ((result = detectValue(sample[startIndex + i])) != null) {
                    return result;
                }
            }
        }
        else {
            for (var i = 0; i < data.length && i < maxLoop; i++) {
                var row = data[startIndex + i];
                if (row && (result = detectValue(row[dimIndex])) != null) {
                    return result;
                }
            }
        }
    }
    else if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS) {
        if (!dimName) {
            return;
        }
        for (var i = 0; i < data.length && i < maxLoop; i++) {
            var item = data[i];
            if (item && (result = detectValue(item[dimName])) != null) {
                return result;
            }
        }
    }
    else if (sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
        if (!dimName) {
            return;
        }
        var sample = data[dimName];
        if (!sample || isTypedArray(sample)) {
            return false;
        }
        for (var i = 0; i < sample.length && i < maxLoop; i++) {
            if ((result = detectValue(sample[i])) != null) {
                return result;
            }
        }
    }
    else if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
        for (var i = 0; i < data.length && i < maxLoop; i++) {
            var item = data[i];
            var val = getDataItemValue(item);
            if (!isArray(val)) {
                return false;
            }
            if ((result = detectValue(val[dimIndex])) != null) {
                return result;
            }
        }
    }

    function detectValue(val) {
        // Consider usage convenience, '1', '2' will be treated as "number".
        // `isFinit('')` get `true`.
        if (val != null && isFinite(val) && val !== '') {
            return false;
        }
        else if (isString(val) && val !== '-') {
            return true;
        }
    }

    return false;
}
336108
function findFirstOverlappingChunk(chunks, windowStartIndex, windowEndIndex)
{
  var helper = function(chunks,
                        chunkIndexLeft,
                        chunkIndexRight,
                        windowStartIndex,
                        windowEndIndex)
  {
    var result;
    var chunkIndexMiddle;
    var middleChunk;
    var middleChunkEndIndex;

    // initialize the return value to -1
    result = -1;

    // compute the index of the middle chunk and get the middle chunk
    chunkIndexMiddle = Math.floor((chunkIndexLeft + chunkIndexRight) / 2);
    middleChunk = chunks[chunkIndexMiddle];

    // if we have two or fewer chunks
    if ((chunkIndexMiddle === chunkIndexLeft) ||
        (chunkIndexMiddle === chunkIndexRight))
    {
      // if we have just one chunk, and it overlaps with the specified window,
      // we've found the chunk we were looking for
      if (chunkIndexLeft === chunkIndexRight)
      {
        if (middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }
      }
      else // we just have two chunks left to check
      {
        // if the first chunk overlaps with the specified window, that's the
        // chunk we were looking for
        if (chunks[chunkIndexLeft].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }

        // otherwise, if the second chunk overlaps with the specified window,
        // that's the chunk we were looking for
        else if (chunks[chunkIndexRight].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexRight;
        }
      }

      return result;
    }

    // if the middle chunk does not overlap with the specified window
    if (!middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
    {
      middleChunkEndIndex = middleChunk.getEndIndex();

      // if the window is to the right of the middle chunk,
      // recurse on the right half
      if (windowStartIndex > middleChunkEndIndex)
      {
        return helper(
            chunks,
            chunkIndexMiddle,
            chunkIndexRight,
            windowStartIndex,
            windowEndIndex);
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
    else
    {
      // if the middle chunk overlaps but the chunk before it does not, the
      // middle chunk is the one we were looking
      if ((chunkIndexMiddle === 0) ||
          !chunks[chunkIndexMiddle - 1].overlapsWithWindow(
              windowStartIndex, windowEndIndex))
      {
        return chunkIndexMiddle;
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
  };

  return helper(chunks, 0, chunks.length - 1, windowStartIndex, windowEndIndex);
}
908374
static void applyOrdering(
            List<NodeInfo> order,
            Element compViewParent,
            Element positionSet,
            NodeInfoTracker tracker) {

        // first pull out all visible channel or visible folder children and
        // put their id's in a list of available children and record their
        // relative order in the CVP.

        final Map<String, NodeInfo> available = new LinkedHashMap<String, NodeInfo>();

        Element child = (Element) compViewParent.getFirstChild();
        Element next = null;
        int indexInCVP = 0;

        while (child != null) {
            next = (Element) child.getNextSibling();

            if (child.getAttribute("hidden").equals("false")
                    && (!child.getAttribute("chanID").equals("")
                            || child.getAttribute("type").equals("regular"))) {
                final NodeInfo nodeInfo = new NodeInfo(child, indexInCVP++);

                tracker.track(order, compViewParent, positionSet);

                final NodeInfo prevNode = available.put(nodeInfo.getId(), nodeInfo);
                if (prevNode != null) {
                    throw new IllegalStateException(
                            "Infinite loop detected in layout. Triggered by "
                                    + nodeInfo.getId()
                                    + " with already visited node ids: "
                                    + available.keySet());
                }
            }
            child = next;
        }

        // now fill the order list using id's from the position set if nodes
        // having those ids exist in the composite view. Otherwise discard
        // that position directive. As they are added to the list remove them
        // from the available nodes in the parent.

        Document CV = compViewParent.getOwnerDocument();
        Element directive = (Element) positionSet.getFirstChild();

        while (directive != null) {
            next = (Element) directive.getNextSibling();

            // id of child to move is in the name attrib on the position nodes
            String id = directive.getAttribute("name");
            child = CV.getElementById(id);

            if (child != null) {
                // look for the NodeInfo for this node in the available
                // nodes and if found use that one. Otherwise use a new that
                // does not include an index in the CVP parent. In either case
                // indicate the position directive responsible for placing this
                // NodeInfo object in the list.

                final String childId = child.getAttribute(Constants.ATT_ID);
                NodeInfo ni = available.remove(childId);
                if (ni == null) {
                    ni = new NodeInfo(child);
                    tracker.track(order, compViewParent, positionSet);
                }

                ni.setPositionDirective(directive);
                order.add(ni);
            }
            directive = next;
        }

        // now append any remaining ids from the available list maintaining
        // the order that they have there.

        order.addAll(available.values());
    }
774759
function compareAscending(a, b) {
  var ai = a.idx,
      bi = b.idx;

  a = a.criteria;
  b = b.criteria;

  // ensure a stable sort in V8 and other engines
  // http://code.google.com/p/v8/issues/detail?id=90
  if (a !== b) {
    if (a > b || typeof a === 'undefined') {
      return 1;
    }
    /*istanbul ignore next*/
    if (a < b || typeof b === 'undefined') {
      return -1;
    }
  }

  /*istanbul ignore next*/
  return ai < bi ? -1 : 1;
}