Closest Plane in Ray Pick

So, in my program, I just have a simple Ray Casting method:

float[] matModelView = new float[16], matProjView = new float[16];
        int[] view = new int[16];
        Vector3f start = new Vector3f();
        Vector3f end = new Vector3f();

        FloatBuffer modelBuffer = compileBuffer(matModelView);
        FloatBuffer projBuffer = compileBuffer(matProjView);
        FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});
        FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});
        IntBuffer viewBuffer = compileBuffer(view);

        glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
        glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
        glGetInteger(GL_VIEWPORT, viewBuffer);

        gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
        gluUnProject(mouseX, mouseY, 1f, modelBuffer, projBuffer, viewBuffer, endBuffer);
        
        this.start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
        this.end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));

And I handle all intersections perfectly. The only problem is getting the closest intersection, I am able to get the closest intersection when I am in an open area, but if an intersect able plane is behind me, and closer that the one in front of me, I have a simple method to determine the closest plane:

float distance = (float) Math.abs(intersection.z - ray.getStart().z);

I assume the problem is with the absolute value, but this is the simplest way I can deal with distance, since you can rotate, if I didn’t call the absolute value method, it would glitch even more.