Given Z and NPOLYS, with len(Z)==sum(npolys), return a 2-element list [LIST, VLIST] such that take(Z, VLIST) and take (NPOLYS, LIST) are sorted from smallest average Z to largest average Z, where the averages are taken over the clusters of length NPOLYS. Within each cluster (polygon), the cyclic order of take (Z, VLIST) remains unchanged, but the absolute order may change.
This sorting order produces correct or nearly correct order for a plfp call to make a plot involving hidden or partially hidden surfaces in three dimensions. It works best when the polygons form a set of disjoint closed, convex surfaces, and when the surface normal changes only very little between neighboring polygons. (If the latter condition holds, then even if sort3d mis-orders two neighboring polygons, their colors will be very nearly the same, and the mistake won't be noticeable.) A truly correct 3D sorting routine is impossible, since there may be no rendering order which produces correct surface hiding (some polygons may need to be split into pieces in order to do that). There are more nearly correct algorithms than this, but they are much slower.