In the combinatorial study of the coefficients of a bivariate polynomial that generalizes both the length and the reflection length generating functions for finite Coxeter groups, Petersen introduced a new Mahonian statistic sor, called the sorting index. Petersen proved that the pairs of statistics (sor, cyc) and (inv, rl-min) have the same joint distribution over the symmetric group, and asked for a combinatorial proof of this fact. In answer to the question of Petersen, we observe a connection between the sorting index and the B-code of a permutation defined by Foata and Han, and we show that the bijection of Foata and Han serves the purpose of mapping (inv, rl-min) to (sor, cyc). We also give a type B analogue of the Foata-Han bijection, and we derive the equidistribution of (inv B , Lmap B , Rmil B ) and (sor B , Lmap B , Cyc B ) over signed permutations. So we get a combinatorial interpretation of Petersen's equidistribution of (inv B , nmin B ) and (sor B , l ′ B ). Moreover, we show that the six pairs of set-valued statistics (Cyc B , Rmil B ), (Cyc B , Lmap B ), (Rmil B , Lmap B ), (Lmap B , Rmil B ), (Lmap B , Cyc B ) and (Rmil B , Cyc B ) are equidistributed over signed permutations.For Coxeter groups of type D, Petersen showed that the two statistics inv D and sor D are equidistributed. We introduce two statistics nmin D andl ′ D for elements of D n and we prove that the two pairs of statistics (inv D , nmin D ) and (sor D ,l ′ D ) are equidistributed.