# Sorting images in a stack based on dicom header

**URL:** <https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669>\
**Category:** Support\
**Created:** [October 4, 2019, 3:44am UTC](https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669 "2019-10-04T03:44:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [October 4, 2019, 3:44am UTC](https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669/1 "2019-10-04T03:44:35Z")

</div>

Hi everyone,

I am working with a stack of 25 DTI images taken in 26 gradient directions, this ultimately comes to 650 dicom files in each stack. In the metadata, I was moving through files from 1 to 650 and observing the b values per file. From this I found that the files in the stack are not ordered properly, is there anyway I can automatically reorder the files according to their specific header?

Thank you!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 4, 2019, 4:34am UTC](https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669/2 "2019-10-04T04:34:46Z")

</div>

All DICOM files are processed based on the file contents, not the file name or order in the database. So, there is no need to reorder the files.

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [October 4, 2019, 6:47am UTC](https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669/3 "2019-10-04T06:47:52Z")

</div>

The reason why I suspect they are incorrectly ordered is the b=0 occurs randomly through the 1-650 files. If they were ordered correctly, the b=0 value could correspond to every 25th .dcm file. There is no pattern in the b=0 values, have you seen this before?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 4, 2019, 1:10pm UTC](https://discourse.slicer.org/t/sorting-images-in-a-stack-based-on-dicom-header/8669/4 "2019-10-04T13:10:18Z")

</div>

You can try converting the data using [dcm2nnix](https://github.com/SlicerDMRI/SlicerDcm2nii). It will interpret dicom from almost any diffusion scan for processing.

Note that when you scan through files it may appear random because the filenames and sort-order of directories may not match the sequence parameters. This is normal. Once you have DWI in Slicer, you can look at the baseline and gradient volumes and it should make sense there.
