code to make histogram for data and sort for any z range