Program: 72163
Project: 72163
Buggy source:
private void addSorting(ICubeQueryDefinition cubeQuery,CubeHandle cube,SeriesDefinition sd,int i) throws BirtException {
  if (sd.getSortKey() == null) {
    return;
  }
  String sortKey=sd.getSortKey().getDefinition();
  if (sd.isSetSorting() && sortKey != null && sortKey.length() > 0) {
    String sortKeyBinding=ChartXTabUtil.getBindingName(sd.getSortKey().getDefinition(),true);
    if (registeredLevels.containsKey(sortKeyBinding)) {
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(sortKey,registeredLevels.get(sortKeyBinding),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
 else     if (registeredMeasures.containsKey(sortKeyBinding)) {
      Query targetQuery=i > 0 ? sd.getQuery() : (Query)sd.getDesignTimeSeries().getDataDefinition().get(0);
      IMeasureDefinition mDef=registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartXTabUtil.getBindingName(targetQuery.getDefinition(),true);
      Binding measureBinding=registeredBindings.get(sortKeyBinding);
      Binding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn(registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),registeredLevels.get(targetBindingName),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
  }
}

Fixed version (ground truth):
private void addSorting(ICubeQueryDefinition cubeQuery,CubeHandle cube,SeriesDefinition sd,int i) throws BirtException {
  if (sd.getSortKey() == null) {
    return;
  }
  String sortKey=sd.getSortKey().getDefinition();
  if (sd.isSetSorting() && sortKey != null && sortKey.length() > 0) {
    String sortKeyBinding=ChartXTabUtil.getBindingName(sd.getSortKey().getDefinition(),true);
    if (registeredLevels.containsKey(sortKeyBinding)) {
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(sortKey,registeredLevels.get(sortKeyBinding),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
 else     if (registeredMeasures.containsKey(sortKeyBinding)) {
      Query targetQuery=i > 0 ? sd.getQuery() : (Query)sd.getDesignTimeSeries().getDataDefinition().get(0);
      IMeasureDefinition mDef=registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartXTabUtil.getBindingName(targetQuery.getDefinition(),true);
      IBinding measureBinding=registeredBindings.get(sortKeyBinding);
      IBinding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn(registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      aggBinding.setExportable(false);
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),registeredLevels.get(targetBindingName),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
  }
}