Program: 73234
Project: 73234
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,(ILevelDefinition)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=(IMeasureDefinition)registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartXTabUtil.getBindingName(targetQuery.getDefinition(),true);
      Binding measureBinding=(Binding)registeredBindings.get(sortKey);
      Binding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn((String)registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),(ILevelDefinition)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,(ILevelDefinition)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=(IMeasureDefinition)registeredMeasures.get(sortKeyBinding);
      String targetBindingName=ChartXTabUtil.getBindingName(targetQuery.getDefinition(),true);
      Binding measureBinding=(Binding)registeredBindings.get(sortKeyBinding);
      Binding aggBinding=new Binding(measureBinding.getBindingName() + targetBindingName);
      aggBinding.setDataType(measureBinding.getDataType());
      aggBinding.setExpression(measureBinding.getExpression());
      aggBinding.addAggregateOn((String)registeredQueries.get(targetBindingName));
      aggBinding.setAggrFunction(mDef.getAggrFunction());
      cubeQuery.addBinding(aggBinding);
      ICubeSortDefinition sortDef=ChartXTabUtil.getCubeElementFactory().createCubeSortDefinition(ExpressionUtil.createJSDataExpression(aggBinding.getBindingName()),(ILevelDefinition)registeredLevels.get(targetBindingName),null,null,sd.getSorting() == SortOption.ASCENDING_LITERAL ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
      cubeQuery.addSort(sortDef);
    }
  }
}