Again a speed question. This time about batching render calls.

When rendering a scene, I can have multiple shaders, textures and vertex buffers. These thins need to switch several times like

Code:
SetTexture 1
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)

SetTexture 2
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)

SetTexture 1
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)
Here the textures is set 3 times, and can be reduced to 2. But what is global the best way to batch? sort by texture, shader of vertex buffer? I personally would say shaders, cause you sometimes need to a matrix or some other stuff. (Im not asking whats best for my app, but just in global ways!)